ScrapeGraphAI是一个创新的Python库,它融合了大型语言模型(LLM)和直接图逻辑,为用户提供了一种高效的方法,用于构建针对网站、文档和XML文件的爬虫流水线。以下是关于ScrapeGraphAI的详细介绍:
ScrapeGraphAI可以在多个领域发挥作用,包括但不限于:
要使用ScrapeGraphAI,用户需要按照以下步骤进行安装和配置:
1、安装Python环境:确保您的计算机上已安装Python,版本>3.8。
2、创建虚拟环境:使用Python的venv模块创建一个虚拟环境,以隔离项目依赖。
- conda create -n Scrapegraph python=3.10 -y
- conda activate Scrapegraph
3、安装ScrapeGraphAI:使用pip命令安装ScrapeGraphAI库和Playwright。
- pip install pytest-playwright
- pip install scrapegraphai
- playwright install
4、配置LLM模型:根据需要配置LLM模型,包括模型名称、温度参数、输出格式等。
本文中采用ollama模型,如何安装请参考网上的教程。
编写抓取脚本:使用ScrapeGraphAI提供的API编写抓取脚本,指定要抓取的网页和数据类型。
以下是一个使用ScrapeGraphAI的SmartScraperGraph类的示例代码,希望抓取csdn博文的关键信息:
- from scrapegraphai.graphs import SmartScraperGraph
-
- graph_config = {
- "llm": {
- "model": "ollama/qwen2:7b",
- "temperature": 0,
- "format": "json", # Ollama needs the format to be specified explicitly
- "base_url": "http://10.33.19.16:11434", # set Ollama URL
- },
- "embeddings": {
- "model": "ollama/quentinz/bge-large-zh-v1.5:lates",
- "base_url": "http://10.33.19.16:11434", # set Ollama URL
- },
- "verbose": True,
- }
-
- smart_scraper_graph = SmartScraperGraph(
- prompt="获取文章的标题、作者、发布时间、阅读量、点赞数、收藏数",
- # also accepts a string with the already downloaded HTML code
- source="https://blog.csdn.net/m0_59235945/article/details/140333596",
- config=graph_config
- )
-
- result = smart_scraper_graph.run()
- print(result)
运行结果如下(通过json格式输出):

原始博文如下:

对比发现,该工具很多属性都解析错误了,而且最大的问题就是它将原文内容进行了润色翻译,导致严重失真。所以该工具的实用场景有待进一步论证,总体来讲,还有很多无法解决的问题。