当前位置: 首页 > article >正文

当 AI 遇上爬虫:让数据提取变得前所未有地简单!

转自:AI真好玩

你是否曾想过,如果有一个工具,能够理解你的意图并自动执行复杂的网络数据抓取任务,那会怎样?ScrapeGraphAI[1] 就是这样一个工具,它利用最新的人工智能技术,让数据提取变得前所未有地简单。

图片

ScrapeGraphAI 是一个用于网络抓取 Python 库,它使用大语言模型(LLM)和直接图为网站、文档和 XML 文件创建抓取管道。只需说出您想要提取哪些信息,它就会为您完成!

图片

工具特点

  • 简单易用:只需输入 API 密钥,您就可以在几秒钟内抓取数千个网页!

  • 开发便捷:你只需要实现几行代码,工作就完成了。

  • 专注业务:有了这个库,您可以节省数小时的时间,因为您只需要设置项目,人工智能就会为您完成一切。

快速开始

在线示例

1.官方 Streamlit

https://scrapegraph-ai-demo.streamlit.app/

图片

2.Google Colab

https://colab.research.google.com/drive/1sEZBonBMGP44CtO6GQTwAlL0BGJXjtfd

本地安装

使用 pip 安装 scrapegraphai:

pip install scrapegraphai

此外,您还需要安装 Playwright[2] 抓取客户端渲染(由 JavaScript 动态渲染)的网页:

playwright install

Playwright 是一个强大的 Python 库,仅用一个 API 即可自动执行 Chromium、Firefox、WebKit 等主流浏览器自动化操作。

使用示例

ScrapeGraphAI 支持通过 API 使用不同的 LLM,例如 OpenAI、Groq、Azure 和 Gemini,或使用 Ollama 的本地模型。

ScrapeGraphAI 内置了 3 种网页爬取流程:

  • SmartScraperGraph:仅需要用户提示词和输入源的单页抓取工具;

  • SearchGraph:多页抓取工具,从搜索引擎的前 n 个搜索结果中提取信息;

  • SpeechGraph:单页抓取工具,从网站提取信息并生成音频文件。

示例一:使用 Ollama API 提取信息

from scrapegraphai.graphs import SmartScraperGraph

graph_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",
    # also accepts a string with the already downloaded HTML code
    source="https://perinim.github.io/projects",
    config=graph_config
)

result = smart_scraper_graph.run()
print(result)

示例二:使用 ChatGPT 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",
    # also accepts a string with the already downloaded HTML code
    source="https://perinim.github.io/projects",
    config=graph_config
)

result = smart_scraper_graph.run()
print(result)

示例三:使用 Groq API 提取信息

from scrapegraphai.graphs import SmartScraperGraph
from scrapegraphai.utils import prettify_exec_info

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"

# Define the configuration for the graph
graph_config = {
    "llm": {
        "api_key": GOOGLE_APIKEY,
        "model": "gemini-pro",
    },
}

# Create the SmartScraperGraph instance
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",  # 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",
    # also accepts a string with the already downloaded HTML code
    source="https://perinim.github.io/projects",  
    config=graph_config
)

result = smart_scraper_graph.run()
print(result)

随着 AI 技术的不断发展,将会为传统工具带来很大的机遇和挑战,后续会不断涌现出更多智能化的工具。

参考资料

[1] 

ScrapeGraphAI: https://scrapegraph-doc.onrender.com/

[2] 

Playwright: https://playwright.dev/


http://www.kler.cn/news/362032.html

相关文章:

  • python基础知识汇总
  • KMP 算法
  • 京东大模型革命电商搜推技术:挑战、实践与未来趋势
  • java中double强制转换int引发的OOM问题
  • jetson nano ubuntu20.04安装ros-Noetic
  • OpenResty性能分析:以HelloWorld服务器为例
  • 常见的前端开发面试题及其答案
  • HarmonyOS的DevEcoStudio安装以及初步认识
  • 【Vue】Vue3(1)
  • 如何在springboot3微项目里面用idea批量创建单元测试逻辑
  • Type Approval (认证)
  • 超简洁的B端系统,还是看国外的设计.
  • 基于STM32的粮库检测系统设计
  • Shell脚本:模块引用
  • 基于SpringBoot健康生活助手微信小程序【附源码】
  • Maven3.9.9环境安装配置
  • Vxe vue vxe-table 分享实现打印表格以及同时打印两张表
  • uniapp结合uview-ui创建项目
  • 基于Prompt工程优化bot
  • 养老院网站毕设计算机毕业设计基于SpringBootSSM框架
  • C#使用XML文件的详解及示例
  • python 更换pip源
  • WordPress中最值得推荐的AI插件:进阶级指南
  • 闯关leetcode——111. Minimum Depth of Binary Tree
  • juzigei/基于Java语言的充电桩系统(充电桩小程序+充电桩管理平台)
  • c++初阶数据结构速成