一句话需求,整页数据:Scrapegraph-ai 自然语言爬虫实操指南
【免费下载链接】Scrapegraph-aiPython scraper based on AI项目地址: https://gitcode.com/GitHub_Trending/sc/Scrapegraph-ai
Scrapegraph-ai 是一个基于大语言模型的 AI 爬虫框架:你只需用自然语言描述要什么数据,它就能把网页解析成结构化 JSON,省去手写 CSS 选择器和 XPath 的繁琐。本文带你走完从环境配置、写出第一个爬虫,到扩展到真实业务的完整路径。
🕷️ 它到底解决什么问题
假设你有一篇新闻页,想拿到页面上所有标题和对应的发布日期。传统做法是 BeautifulSoup:你得先打开浏览器、逐层定位到div.news > h2、再挨个取值;页面稍微改版,选择器就全线失效。Selenium 更重,还要操心驱动、等待和渲染时机。
Scrapegraph-ai 的思路完全不同。它基于 LangChain,把抓取过程拆成一串可配置的节点(fetch 网页 → parse 解析 → rag 检索 → generate 生成答案),你只负责用自然语言说出想要什么,剩下的页面理解和结构化交给 LLM 完成。它和传统方案的本质区别,在于「你要精确告诉程序数据在哪」变成了「程序自己看懂页面、按你的描述提取」。
🐍 环境与模型选择
如果你只想最快跑通,用 Ollama 本地模型即可,零费用、不依赖任何 API 密钥。云端模型(OpenAI、Gemini、Anthropic 等)能力更强、延迟更低,但需要配置对应密钥;两者都通过同一个graph_config切换,只是llm段不同。
环境上唯一要记住的一点:需要 Python 3.10 及以上版本,建议用虚拟环境隔离,避免依赖冲突。
先在虚拟环境里安装核心包,再装 Playwright 浏览器内核(抓取网页内容依赖它):
python3 -m venv sgai-env source sgai-env/bin/activate pip install scrapegraphai playwright install本地模型方案里,先让 Ollama 把模型拉到本地:
ollama pull llama3.2装好后 Ollama 会默认监听http://localhost:11434,无需额外配置。
🚀 写你的第一个爬虫:从 prompt 到 JSON
第一个爬虫按「写配置 → 初始化 → 执行」三步走。配置里llm指定本地模型,prompt用自然语言写清需求,source是要抓的 URL,最后调用run()拿到结果。
from scrapegraphai.graphs import SmartScraperGraph import json graph_config = { "llm": { "model": "ollama/llama3.2", "temperature": 0, "model_tokens": 4096, }, "verbose": True, "headless": False, } smart_scraper_graph = SmartScraperGraph( prompt="提取页面上所有新闻的标题和发布日期", source="https://example-news-site.com/latest", config=graph_config, ) result = smart_scraper_graph.run() print(json.dumps(result, indent=4))预期你会看到类似这样的 JSON 输出:
{ "titles": ["示例新闻标题一", "示例新闻标题二"], "dates": ["2025-08-12", "2025-08-15"] }整个过程 SmartScraperGraph 内部把 URL 与你的 prompt 交给四个节点依次处理,最终产出结构化结果:
换一种任务,换一种 Graph
如果你的任务不是抓单个页面,而是跨页搜索、批量处理或解析本地文件,框架里每种任务都对应一个专门的 Graph 类,切换入口即可。
搜索增强场景下,SearchGraph会先查搜索引擎、再对前几篇结果并行抓取,用max_results控制篇数:
from scrapegraphai.graphs import SearchGraph search_graph = SearchGraph( prompt="列出近期低空经济的主流应用场景", config={"llm": {"model": "ollama/llama3.2"}, "max_results": 3}, ) result = search_graph.run()本地文档解析用DocumentScraperGraph,source可以直接传一段文本或一个 HTML/文本文件路径:
from scrapegraphai.graphs import DocumentScraperGraph doc_graph = DocumentScraperGraph( prompt="总结这篇文档的核心论点", source="report.html", config={"llm": {"model": "ollama/llama3.2"}}, ) result = doc_graph.run()常用 Graph 速查:
| 任务类型 | 推荐 Graph | 关键参数 |
|---|---|---|
| 单页提取 | SmartScraperGraph | prompt、source |
| 多页批量 | SmartScraperMultiGraph | source(列表)、schema |
| 搜索增强 | SearchGraph | max_results |
| 本地文档解析 | DocumentScraperGraph | source(文本/文件) |
| 深度多页搜索 | DepthSearchGraph | depth |
| 生成抓取脚本 | ScriptCreatorGraph | source |
跑不通时先检查这三处
大多数报错集中在三类:版本与依赖、模型加载、目标站反爬。
版本依赖这块,先确认 Python 是 3.10 及以上、且在干净的虚拟环境里。如果你换了模型或升级过包后报ImportError、VersionConflict,把虚拟环境删掉重建、再pip install --upgrade scrapegraphai,多数能解决。
模型加载失败通常是本地模型没拉取或密钥没配。本地方案先跑ollama pull llama3.2;云端方案检查llm段里api_key是否填了、model是否带上了厂商前缀(比如openai/gpt-4o-mini,而不是裸的gpt-4o-mini)。如果你改了模型名还是报错,打开verbose: True看具体节点抛在哪。
目标站反爬方面,把headless设为False先肉眼观察页面是否真的加载出来了,再考虑在loader_kwargs里挂代理、或换更轻量的 Lite 版本 Graph。
⚙️ 往生产走:几个值得知道的配置
真正投产时,有三个配置最常用。verbose: True会在控制台打印各节点调试信息,定位问题很快;cache_path指向一个目录后,命中过的页面会从缓存读取,避免重复抓取;反爬压力大的站点,在loader_kwargs里挂代理最实际。
graph_config = { "llm": {"model": "ollama/llama3.2"}, "verbose": True, "headless": False, "cache_path": "./cache", "loader_kwargs": { "proxy": {"server": "http://your_proxy:port"} }, }批量抓多个 URL 时换成SmartScraperMultiGraph,source传一个列表即可并行处理:
from scrapegraphai.graphs import SmartScraperMultiGraph graph = SmartScraperMultiGraph( prompt="提取每篇文章的标题与摘要", source=["url1", "url2", "url3"], config=graph_config, ) result = graph.run()更多现成示例可以直接参考 官方 examples 目录,配置项含义详见 Graph 配置说明 与 LLM 模型配置。
Scrapegraph-ai 的思路是让你专注描述需求、把解析交给模型;想跟进新 Graph 和能力变化,可以定期查看 CHANGELOG。
【免费下载链接】Scrapegraph-aiPython scraper based on AI项目地址: https://gitcode.com/GitHub_Trending/sc/Scrapegraph-ai
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考