ScrapeGraphAI:基于大语言模型的智能网络爬虫工具
你是否曾想过,如果有一个工具能够理解你的意图并自动执行复杂的网络数据抓取任务,那会怎样?ScrapeGraphAI 正是这样一个工具。它利用最新的人工智能技术(LLM),让数据提取变得前所未有地简单。
工具概述
ScrapeGraphAI 是一个用于网络抓取的 Python 库。它使用大语言模型(LLM)和直接图(Direct Graph)为网站、文档和 XML 文件创建抓取管道。用户只需输入想要提取的信息描述(Prompt),系统便会自动解析页面结构并返回结果。
核心优势
- 简单易用:只需配置 API 密钥,即可在几秒钟内处理大量网页。
- 开发便捷:仅需几行代码即可完成复杂的数据清洗与提取工作。
- 专注业务逻辑:节省数小时的手动编写解析器时间,将精力集中在业务需求上。
快速开始
环境准备
确保已安装 Python 3.8+ 环境。
1. 安装库
使用 pip 安装 scrapegraphai:
pip install scrapegraphai
2. 安装浏览器自动化工具
为了支持客户端渲染(由 JavaScript 动态渲染)的网页,需要安装 Playwright:
playwright install
Playwright 是一个强大的 Python 库,仅用一个 API 即可自动执行 Chromium、Firefox、WebKit 等主流浏览器的自动化操作。
在线示例
官方提供了 Streamlit 演示和 Google Colab 笔记本供快速体验:
核心功能模块
ScrapeGraphAI 内置了三种主要的网页爬取流程,适用于不同场景:
- SmartScraperGraph:单页抓取工具。仅需用户提示词和输入源,适合从特定 URL 提取结构化数据。
- SearchGraph:多页抓取工具。从搜索引擎的前 n 个搜索结果中提取信息,适合竞品分析或市场调研。
- SpeechGraph:单页抓取工具。从网站提取信息并生成音频文件,适合无障碍访问或内容播报。
使用示例
ScrapeGraphAI 支持通过 API 使用不同的 LLM,例如 OpenAI、Groq、Azure 和 Gemini,或使用 Ollama 的本地模型。
示例一:使用 Ollama API 提取信息
此示例展示了如何配置本地运行的 Ollama 模型进行数据提取。
from scrapegraphai.graphs import SmartScraperGraph
import os
graph_config = {
"llm": {
"model": "ollama/mistral",
"temperature": 0,
"format": "json",
"base_url": "http://localhost:11434",
},
"embeddings": {
"model": "ollama/nomic-embed-text",
"base_url": "http://localhost:11434",
}
}
smart_scraper_graph = SmartScraperGraph(
prompt="List me all the articles",
source="https://perinim.github.io/projects",
config=graph_config
)
result = smart_scraper_graph.run()
print(result)
示例二:使用 ChatGPT (OpenAI) API 提取信息
from scrapegraphai.graphs import SmartScraperGraph
OPENAI_API_KEY = "YOUR_API_KEY"
graph_config = {
"llm": {
"api_key": OPENAI_API_KEY,
"model": "gpt-3.5-turbo",
}
}
smart_scraper_graph = SmartScraperGraph(
prompt="List me all the articles",
source="https://perinim.github.io/projects",
config=graph_config
)
result = smart_scraper_graph.run()
print(result)
示例三:使用 Groq API 提取信息
Groq 提供极快的推理速度,适合对延迟敏感的场景。
from scrapegraphai.graphs import SmartScraperGraph
from scrapegraphai.utils import prettify_exec_info
import os
groq_key = os.getenv("GROQ_APIKEY")
graph_config = {
"llm": {
"model": "groq/gemma-7b-it",
"api_key": groq_key,
"temperature": 0
},
"embeddings": {
"model": "ollama/nomic-embed-text",
"temperature": 0,
"base_url": "http://localhost:11434",
},
"headless": False
}
smart_scraper_graph = SmartScraperGraph(
prompt="List me all the projects with their description and the author.",
source="https://perinim.github.io/projects",
config=graph_config
)
result = smart_scraper_graph.run()
print(result)
示例四:使用 Gemini API 提取信息
from scrapegraphai.graphs import SmartScraperGraph
GOOGLE_APIKEY = "YOUR_API_KEY"
graph_config = {
"llm": {
"api_key": GOOGLE_APIKEY,
"model": "gemini-pro",
}
}
smart_scraper_graph = SmartScraperGraph(
prompt="List me all the articles",
source="https://perinim.github.io/projects",
config=graph_config
)
result = smart_scraper_graph.run()
print(result)
示例五:使用 Docker 部署本地模型
在使用本地模型之前,建议先创建 Docker 容器以确保环境一致性。
docker-compose up -d
docker exec -it ollama ollama pull stablelm-zephyr
您可以使用 Ollama 上可用的模型或您自己的模型来代替 stablelm-zephyr。
from scrapegraphai.graphs import SmartScraperGraph
graph_config = {
"llm": {
"model": "ollama/mistral",
"temperature": 0,
"format": "json",
}
}
smart_scraper_graph = SmartScraperGraph(
prompt="List me all the articles",
source="https://perinim.github.io/projects",
config=graph_config
)
result = smart_scraper_graph.run()
print(result)
高级配置与最佳实践
1. 提示词工程 (Prompt Engineering)
ScrapeGraphAI 的效果很大程度上取决于 Prompt 的质量。建议遵循以下原则:
- 明确性:清晰定义需要提取的字段名称和数据类型。
- 结构化:要求输出 JSON 格式,便于后续程序处理。
- 上下文:如果页面包含多个部分,指定具体的区域或条件。
2. 错误处理
在实际生产环境中,网络请求可能会失败或 LLM 可能返回非预期格式。建议添加异常捕获机制:
try:
result = smart_scraper_graph.run()
except Exception as e:
print(f"Error occurred: {e}")
3. 性能优化
- 缓存:对于重复请求的 URL,考虑缓存响应以减少 API 调用成本。
- 并发:ScrapeGraphAI 支持异步操作,可在处理大量 URL 时提高吞吐量。
- 模型选择:对于简单任务,使用轻量级模型(如 Mistral 7B)可显著降低成本;对于复杂逻辑,再使用 GPT-4 等大模型。
总结
随着 AI 技术的不断发展,传统爬虫工具正面临新的机遇与挑战。ScrapeGraphAI 通过将大语言模型集成到数据提取流程中,极大地降低了非结构化数据的处理门槛。未来,更多智能化的工具将会涌现,帮助开发者更高效地应对海量数据处理需求。