Aitrainee | 公众号:AI进修生
🌟ScrapeGraphAI 是一个网络抓取Python 库,它使用 LLM 和直接图形逻辑为网站、文档和 XML 文件创建抓取管道。只需说出您想要提取哪些信息,ScrapeGraphAI就会为你完成!
在当今数据驱动的世界中,网络抓取已成为从广阔的互联网中收集信息的重要工具。然而,传统的网络抓取工具往往难以适应网站的动态特性,需要开发人员不断维护和更新。
输入 ScrapeGraphAI,这是一个革命性的 Python 库,它利用大型语言模型 (LLMs) 的强大功能和直接图形逻辑来创建灵活且适应性强的 Web 抓取管道。
ScrapeGraphAI 代表了网络抓取领域的重大进步,提供了一个开源解决方案,旨在应对当今不断发展的网络环境的挑战。这就是 ScrapeGraphAI 脱颖而出的原因:
直接图逻辑 :此功能使用基于图的方法动态创建爬取管道,确保基于用户定义的提示实现高效的数据检索。
多功能模型和API :ScrapeGraphAI支持各种模型和API,包括OpenAI的GPT、Docker、Groq、Azure等,允许用户根据自己的抓取需求选择最佳选项。
灵活性和适应性 :传统的网页抓取工具通常依赖于固定模式或手动配置来从网页中提取数据。ScrapeGraphAI 由 LLMs 提供支持,可适应网站结构的变化,减少开发人员持续干预的需要。
易于安装 :通过简单的 pip install 命令,用户可以快速设置 ScrapeGraphAI 并开始从网站、文档和 XML 文件中抓取数据。
🚀 快速安装
Scrapegraph-ai 的参考页面可在 pypy 的官方页面上找到:pypi。
pip install scrapegraphai
您还需要安装 Playwright 以进行基于 JavaScript 的爬取:
playwright install
注意 :建议在虚拟环境中安装库,以避免与其他库的冲突 🐱
🔍 演示
官方 streamlit 演示:
https://skillicons.dev/icons?i=react)](https://scrapegraph-ai-demo.streamlit.app/
在网上直接尝试使用 Google Colab:
https://colab.research.google.com/assets/colab-badge.svg
按照以下链接上的步骤设置您的 OpenAI API 密钥:[link]:
https://scrapegraph-ai.readthedocs.io/en/latest/index.html
📖 文档
ScrapeGraphAI 的文档可以在[这里]:
https://scrapegraph-ai.readthedocs.io/en/latest/
还请查看 docusaurus [文档]:
https://scrapegraph-doc.onrender.com/
💻 使用方法
您可以使用 SmartScraper
类通过提示从网站提取信息。
SmartScraper
类是一个直接图实现,使用网页爬取管道中最常见的节点。有关更多信息,请参阅文档。
情况 1:使用 Ollama 提取信息
记得单独在 Ollama 上下载模型!
from
scrapegraphai
.
graphs
import
SmartScraperGraph
graph\_config
=
{
"llm"
:
{
"model"
:
"ollama/mistral"
,
"temperature"
:
0
,
"format"
:
"json"
,
# Ollama 需要显式指定格式
"base\_url"
:
"http://localhost:11434"
,
# 设置 Ollama URL
},
"embeddings"
:
{
"model"
:
"ollama/nomic-embed-text"
,
"base\_url"
:
"http://localhost:11434"
,
# 设置 Ollama URL
}
}
smart\_scraper\_graph
=
SmartScraperGraph
(
prompt
=
"List me all the articles"
,
# 也可以使用已下载的 HTML 代码的字符串
source
=
"https://perinim.github.io/projects"
,
config
=
graph\_config
)
result
=
smart\_scraper\_graph
.
run
()
print
(
result
)
情况 2:使用 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 需要显式指定格式
# "model\_tokens": 2000, # 设置上下文长度任意
},
}
smart\_scraper\_graph
=
SmartScraperGraph
(
prompt
=
"List me all the articles"
,
# 也可以使用已下载的 HTML 代码的字符串
source
=
"https://perinim.github.io/projects"
,
config
=
graph\_config
)
result
=
smart\_scraper\_graph
.
run
()
print
(
result
)
情况 3:使用 Openai 模型提取信息
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"
,
# 也可以使用已下载的 HTML 代码的字符串
source
=
"https://perinim.github.io/projects"
,
config
=
graph\_config
)
result
=
smart\_scraper\_graph
.
run
()
print
(
result
)
情况 4:使用 Groq 提取信息
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
)
情况 5:使用 Azure 提取信息
from
langchain\_openai
import
AzureChatOpenAI
from
langchain\_openai
import
AzureOpenAIEmbeddings
lm\_model\_instance
=
AzureChatOpenAI
(
openai\_api\_version
=
os
.
environ
[
"AZURE\_OPENAI\_API\_VERSION"
],
azure\_deployment
=
os
.
environ
[
"AZURE\_OPENAI\_CHAT\_DEPLOYMENT\_NAME"
]
)
embedder\_model\_instance
=
AzureOpenAIEmbeddings
(
azure\_deployment
=
os
.
environ
[
"AZURE\_OPENAI\_EMBEDDINGS\_DEPLOYMENT\_NAME"
],
openai\_api\_version
=
os
.
environ
[
"AZURE\_OPENAI\_API\_VERSION"
],
)
graph\_config
=
{
"llm"
:
{
"model\_instance"
:
llm\_model\_instance
},
"embeddings"
:
{
"model\_instance"
:
embedder\_model\_instance
}
}
smart\_scraper\_graph
=
SmartScraperGraph
(
prompt
=
"""List me all the events, with the following fields: company\_name, event\_name, event\_start\_date, event\_start\_time,
event\_end\_date, event\_end\_time, location, event\_mode, event\_category,
third\_party\_redirect, no\_of\_days,
time\_in\_hours, hosted\_or\_attending, refreshments\_type,
registration\_available, registration\_link"""
,
source
=
"https://www.hmhco.com/event"
,
config
=
graph\_config
)
情况 6:使用 Gemini 提取信息
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
)
所有 3 个情况的输出将是一个包含提取信息的字典,例如:
{
'titles'
:
[
'Rotary Pendulum RL'
],
'descriptions'
:
[
'Open Source project aimed at controlling a real life rotary pendulum using RL algorithms'
]
}
参考链接
Github: https://github.com/VinciGit00/Scrapegraph-ai?tab=readme-ov-file Colab Notebook: https://colab.research.google.com/drive/1sEZBonBMGP44CtO6GQTwAlL0BGJXjtfd?usp=sharing
— 完 —
点这里👇关注我,记得标星哦~
一键三连「分享」、「点赞」和「在看」
科技前沿进展日日相见 ~