ScrapeGraphAI:用LLM和大语言模型实现智能网页爬取,Python开发者的新一代爬虫利器

今天推荐的是ScrapeGraphAI,这是一个用Python开发的智能网页爬虫库。它通过LLM和大语言模型自动理解网页结构,只需描述你想提取什么信息,AI就会自动完成爬取。Star已突破23000+,更新非常活跃。

项目介绍

ScrapeGraphAI是一个革命性的Python网页爬虫库,它彻底颠覆了传统爬虫的开发模式。传统爬虫需要手动分析网页结构、写CSS选择器或XPath表达式,而ScrapeGraphAI只需要你用自然语言描述想要提取的信息,AI就会自动分析网页并提取数据。

GitHub:https://github.com/VinciGit00/Scrapegraph-ai
Stars:23284+ | 语言:Python | 协议:MIT

核心特色

1. 自然语言驱动:只需用自然语言描述需求,如”提取所有产品价格和名称”,AI自动分析并提取。

2. 多管道支持:SmartScraperGraph(单页爬取)、SearchGraph(多页搜索爬取)、SpeechGraph(爬取并生成音频)、ScriptCreatorGraph(爬取并生成Python脚本)等多种管道。

3. 多LLM支持:支持OpenAI GPT、Claude、Gemini、MiniMax等商业API,也支持通过Ollama使用本地模型(如Llama3)。

4. LangChain/LlamaIndex集成:可无缝集成到LangChain和LlamaIndex生态系统中。

5. MCP协议支持:提供MCP服务器,可直接接入各类AI Agent框架。

6. 支持本地文档:除了网页,还支持从本地HTML、XML、JSON、Markdown文档中提取数据。

7. 并行处理:多版本管道支持LLM并行调用,大幅提升爬取效率。

安装步骤

方式一:pip安装(推荐)

pip install scrapegraphai

# 安装Playwright(用于抓取动态网页内容)
playwright install

方式二:使用虚拟环境

python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate
pip install scrapegraphai
playwright install

方式三:源码安装

git clone https://github.com/VinciGit00/Scrapegraph-ai.git
cd Scrapegraph-ai
pip install -e .

使用方法

基础示例:单页爬取

from scrapegraphai.graphs import SmartScraperGraph

graph_config = {
    "llm": {
        "api_key": "YOUR_OPENAI_API_KEY",
        "model": "openai/gpt-4o-mini",
    },
    "verbose": True,
    "headless": True,
}

smart_scraper_graph = SmartScraperGraph(
    prompt="提取网页上所有产品的名称、价格和描述",
    source="https://example.com/products",
    config=graph_config
)

result = smart_scraper_graph.run()
print(result)

使用本地模型(Ollama)

from scrapegraphai.graphs import SmartScraperGraph

graph_config = {
    "llm": {
        "model": "ollama/llama3.2",
        "model_tokens": 8192,
    },
    "verbose": True,
    "headless": False,
}

smart_scraper_graph = SmartScraperGraph(
    prompt="提取公司名称、创始人和社交媒体链接",
    source="https://scrapegraphai.com/",
    config=graph_config
)

result = smart_scraper_graph.run()
print(result)

多页搜索爬取(SearchGraph)

from scrapegraphai.graphs import SearchGraph

search_graph = SearchGraph(
    prompt="查找2026年最新的人工智能发展趋势",
    config={
        "llm": {"model": "openai/gpt-4o-mini", "api_key": "YOUR_KEY"},
        "search_engine": "duck-duck-swap",
    }
)

result = search_graph.run()
print(result)

生成爬虫脚本(ScriptCreatorGraph)

from scrapegraphai.graphs import ScriptCreatorGraph

script_graph = ScriptCreatorGraph(
    prompt="创建一个爬虫,从页面提取所有新闻标题和发布日期",
    source="https://news.example.com",
    config=graph_config
)

result = script_graph.run()
print(result)  # 输出可复用的Python脚本

适用场景

1. 电商数据采集:自动采集竞品价格、评论、评分等数据,无需手动分析网页结构。

2. 新闻聚合平台:自动从多个新闻网站提取文章内容,构建个性化新闻聚合服务。

3. RAG知识库构建:为RAG系统自动抓取网络数据,作为LLM的实时知识来源。

4. 竞品情报监控:监控竞品网站的产品更新、价格变动、内容变化等。

5. AI数据管道:作为AI Agent的数据获取层,让AI自主搜索和提取所需信息。

6. 学术数据收集:自动从学术网站提取论文摘要、引用信息等。

开源协议

MIT开源协议,完全免费使用,可商业应用。

总结

ScrapeGraphAI代表了AI时代爬虫技术的未来——不再需要手动分析网页结构,只需要用自然语言描述需求,AI就会帮你完成所有工作。它特别适合需要快速采集数据但不想花时间分析网页结构的开发者。结合LangChain或LlamaIndex,可以构建强大的AI驱动数据管道。


服务支持:如有兴趣不会搭建,可以联系微信:WRYD6166,开源项目搭建10-50元。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容