top of page

用 ScrapeGraphAI 简化网页抓取:发挥大语言模型的力量

9月15日
讀畢需時 3 分鐘

2024年5月22日

网页抓取,即从网站中提取数据的过程,已成为研究人员、数据科学家和企业不可或缺的工具。然而,网页结构的复杂性以及对定制抓取逻辑的需求往往带来巨大挑战。ScrapeGraphAI 是一个 Python 库,旨在借助大语言模型(LLM)和直接图逻辑的力量,彻底改变网页抓取的方式。使用 ScrapeGraphAI,用户只需描述想要提取的信息,便可轻松为网站、文档和 XML 文件创建抓取流程。

ScrapeGraphAI 在研究中有广泛的应用,尤其适用于从多个在线来源收集数据。以下是 ScrapeGraphAI 能够发挥重要作用的几种场景:

文献综述:研究人员可以使用 ScrapeGraphAI 从学术论文、期刊和会议论文集中提取相关信息,从而简化文献综述流程。

市场分析:通过抓取电子商务网站、社交媒体平台和新闻文章中的数据,研究人员可以为市场分析和消费者行为研究获取有价值的洞见。

情感分析:ScrapeGraphAI 可用于抓取各类网站上的用户评论、留言和观点,使研究人员能够对大规模数据集开展情感分析。

如何使用 ScrapeGraphAI:ScrapeGraphAI 使用简便,只需极少的配置。

按照以下步骤开始:

使用 pip 安装 ScrapeGraphAI:

pip install scrapegraphai

安装 Playwright,用于基于 JavaScript 的抓取:

playwright install

设置 OpenAI API 密钥(如使用 OpenAI 模型)。

从 ScrapeGraphAI 提供的三种主要抓取流程中选择一种:

A. SmartScraperGraph:单页抓取器,需要用户提示词和输入源。

B. SearchGraph:多页抓取器,从搜索引擎排名靠前的搜索结果中提取信息。

C. SpeechGraph:单页抓取器,从网站提取信息并生成音频文件。

通过指定 LLM、嵌入模型及其他相关设置来配置抓取流程。

使用所需的提示词和数据源运行抓取流程,并获取提取的信息。

案例 1:使用本地模型的 SmartScraper。请记得先安装 Ollama,并使用 ollama pull 命令下载模型。

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

},

"verbose": True,

}

smart_scraper_graph = SmartScraperGraph(

prompt="List me all the projects with their descriptions",

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)

输出将是一个包含项目及其描述的列表,形式如下:

{'projects': [{'title': 'Rotary Pendulum RL', 'description': 'Open Source project aimed at controlling a real life rotary pendulum using RL algorithms'}, {'title': 'DQN Implementation from scratch', 'description': 'Developed a Deep Q-Network algorithm to train a simple and double pendulum'}, ...]}

案例 2:使用混合模型的 SearchGraph。我们使用 Groq 作为 LLM,使用 Ollama 生成嵌入。

from scrapegraphai.graphs import SearchGraph

Define the configuration for the graph

graph_config = {

"llm": {

"model": "groq/gemma-7b-it",

"api_key": "GROQ_API_KEY",

"temperature": 0

},

"embeddings": {

"model": "ollama/nomic-embed-text",

"base_url": "http://localhost:11434", # set ollama URL arbitrarily

},

"max_results": 5,

}

Create the SearchGraph instance

search_graph = SearchGraph(

prompt="List me all the traditional recipes from Chioggia",

config=graph_config

)

Run the graph

result = search_graph.run()

print(result)

输出将是一个食谱列表,形式如下:

{'recipes': [{'name': 'Sarde in Saòre'}, {'name': 'Bigoli in salsa'}, {'name': 'Seppie in umido'}, {'name': 'Moleche frite'}, {'name': 'Risotto alla pescatora'}, {'name': 'Broeto'}, {'name': 'Bibarasse in Cassopipa'}, {'name': 'Risi e bisi'}, {'name': 'Smegiassa Ciosota'}]}

案例 3:使用 OpenAI 的 SpeechGraph。只需传入 OpenAI API 密钥和模型名称即可。

from scrapegraphai.graphs import SpeechGraph

graph_config = {

"llm": {

"api_key": "OPENAI_API_KEY",

"model": "gpt-3.5-turbo",

},

"tts_model": {

"api_key": "OPENAI_API_KEY",

"model": "tts-1",

"voice": "alloy"

},

"output_path": "audio_summary.mp3",

}

************

Create the SpeechGraph instance and run it

************

speech_graph = SpeechGraph(

prompt="Make a detailed audio summary of the projects.",

source="https://perinim.github.io/projects/",

config=graph_config,

)

result = speech_graph.run()

print(result)

问题与注意事项:尽管 ScrapeGraphAI 为网页抓取提供了强大而直观的方法,但仍需注意以下几点:

遵守网站服务条款和 robots.txt:确保抓取活动符合网站的服务条款,且不违反任何法律或伦理准则。

API 使用与成本:使用 OpenAI 或 Groq 等第三方 API 时,应留意相关费用和使用限制。

数据质量与可靠性:提取信息的准确性取决于 LLM 的质量以及用户提示词的清晰程度。在将抓取的数据用于关键应用之前,务必对其进行验证。

ScrapeGraphAI 代表了网页抓取领域的重大进步,使用户能够轻松地从网站中提取信息。通过发挥大语言模型和直接图逻辑的力量,ScrapeGraphAI 简化了抓取过程,为研究人员和数据爱好者开辟了新的可能。无论您是在进行文献综述、分析市场趋势,还是开展情感分析,ScrapeGraphAI 都能为您的数据提取需求提供流畅高效的解决方案。

留言


这项倡议得到了以下组织的支持:

  • Twitter
  • LinkedIn
  • YouTube
logo_edited.png
bottom of page