ScrapeGraphAI 是一个基于Python的Web抓取库,使用大型语言模型和直接图逻辑来创建针对网站、文档和XML文件的抓取流程。用户只需指定想要抽取的信息,该库便能自动完成抓取任务。该项目强调易用性和高效性,支持通过命令行界面或代码实现灵活的数据抓取,并提供丰富的文档支持,帮助用户快速上手。
这是 Scrapegraph-ai 的官方 PyPI 页面的参考信息
https://pypi.org/project/scrapegraphai/
pip install scrapegraphai你还需要安装 Playwright 用于基于 JavaScript 的网页抓取:
playwright install注意:建议在虚拟环境中安装该库,以避免与其他库冲突。
https://scrapegraph-ai-demo.streamlit.app/
你可以使用 SmartScraper 类通过提示从网站中提取信息。SmartScraper 类是一个直接图实现,使用了网页抓取流程中最常见的节点。更多信息请查看文档。
https://scrapegraph-ai.readthedocs.io/en/latest/
案例 1:使用 Ollama 提取信息记得要单独下载 Ollama 的模型!
from scrapegraphai.graphs import SmartScraperGraphgraph_config = { "llm": { "model": "ollama/mistral", "temperature": 0, "format": "json", # Ollama needs the format to be specified explicitly "base_url": "http://localhost:11434" # set Ollama URL }, "embeddings": { "model": "ollama/nomic-embed-text", "base_url": "http://localhost:11434" # set Ollama URL }}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)案例 2:使用 Docker 提取信息
注意:在使用本地模型之前,记得创建 Docker 容器!
docker-compose up -ddocker exec -it ollama ollama pull stablelm-zephyr你可以使用 Ollama 上可用的模型或者你自己的模型,而不是使用 stablelm-zephyr。
from scrapegraphai.graphs import SmartScraperGraphgraph_config = { "llm": { "model": "ollama/mistral", "temperature": 0, "format": "json", # Ollama needs the format to be specified explicitly # "model_tokens": 2000, # set context length arbitrarily },}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)案例 3:使用 OpenAI 模型提取信息
from scrapegraphai.graphs import SmartScraperGraphOPENAI_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)案例 4:使用 Groq 提取信息
from scrapegraphai.graphs import SmartScraperGraphfrom scrapegraphai.utils import prettify_exec_infogroq_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" }, "headers": { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36" }}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(prettify_exec_info(result))