主题
Scrapegraph 抓取工具
ScrapegraphScrapeTool利用 Scrapegraph AI 的 SmartScraper API 智能地从网站中提取内容。
ScrapegraphScrapeTool
描述
ScrapegraphScrapeTool 旨在利用 Scrapegraph AI 的 SmartScraper API 智能地从网站中提取内容。该工具借助 AI 驱动的内容提取提供高级网页抓取能力,非常适合定向数据收集和内容分析任务。与传统网页抓取器不同,它能够理解网页的上下文和结构,从而根据自然语言提示词提取最相关的信息。
安装
要使用此工具,你需要安装 Scrapegraph Python 客户端:
shell
uv add scrapegraph-py你还需要将 Scrapegraph API key 设置为环境变量:
shell
export SCRAPEGRAPH_API_KEY="your_api_key"你可以从 Scrapegraph AI 获取 API key。
入门步骤
要有效使用 ScrapegraphScrapeTool,请按以下步骤操作:
- 安装依赖项:使用上面的命令安装所需的包。
- 设置 API key:将你的 Scrapegraph API key 设置为环境变量,或在初始化时提供它。
- 初始化工具:使用必要的参数创建该工具的实例。
- 定义提取提示词:创建自然语言提示词,以引导对特定内容的提取。
示例
以下示例演示如何使用 ScrapegraphScrapeTool 从网站中提取内容:
python
from crewai import Agent, Task, Crew
from crewai_tools import ScrapegraphScrapeTool
# Initialize the tool
scrape_tool = ScrapegraphScrapeTool(api_key="your_api_key")
# Define an agent that uses the tool
web_scraper_agent = Agent(
role="Web Scraper",
goal="Extract specific information from websites",
backstory="An expert in web scraping who can extract targeted content from web pages.",
tools=[scrape_tool],
verbose=True,
)
# Example task to extract product information from an e-commerce site
scrape_task = Task(
description="Extract product names, prices, and descriptions from the featured products section of example.com.",
expected_output="A structured list of product information including names, prices, and descriptions.",
agent=web_scraper_agent,
)
# Create and run the crew
crew = Crew(agents=[web_scraper_agent], tasks=[scrape_task])
result = crew.kickoff()你也可以使用预定义的参数初始化该工具:
python
# Initialize the tool with predefined parameters
scrape_tool = ScrapegraphScrapeTool(
website_url="https://www.example.com",
user_prompt="Extract all product prices and descriptions",
api_key="your_api_key"
)参数
ScrapegraphScrapeTool 在初始化时接受以下参数:
- api_key:可选。你的 Scrapegraph API key。如果未提供,它将查找
SCRAPEGRAPH_API_KEY环境变量。 - website_url:可选。要抓取的网站 URL。如果在初始化时提供,智能体在使用该工具时就无需再指定它。
- user_prompt:可选。用于内容提取的自定义指令。如果在初始化时提供,智能体在使用该工具时就无需再指定它。
- enable_logging:可选。是否为 Scrapegraph 客户端启用日志记录。默认为
False。
用量
在与智能体一起使用 ScrapegraphScrapeTool 时,智能体需要提供以下参数(除非这些参数已在初始化时指定):
- website_url:要抓取的网站 URL。
- user_prompt:可选。用于内容提取的自定义指令。默认为 “Extract the main content of the webpage”。
该工具将根据所提供的提示词返回提取到的内容。
python
# Example of using the tool with an agent
web_scraper_agent = Agent(
role="Web Scraper",
goal="Extract specific information from websites",
backstory="An expert in web scraping who can extract targeted content from web pages.",
tools=[scrape_tool],
verbose=True,
)
# Create a task for the agent to extract specific content
extract_task = Task(
description="Extract the main heading and summary from example.com",
expected_output="The main heading and summary from the website",
agent=web_scraper_agent,
)
# Run the task
crew = Crew(agents=[web_scraper_agent], tasks=[extract_task])
result = crew.kickoff()错误处理
ScrapegraphScrapeTool 可能抛出以下异常:
- ValueError:当缺少 API key 或 URL 格式无效时。
- RateLimitError:当超过 API 速率限制时。
- RuntimeError:当抓取操作失败时(网络问题、API 错误)。
建议指导智能体优雅地处理潜在错误:
python
# Create a task that includes error handling instructions
robust_extract_task = Task(
description="""
Extract the main heading from example.com.
Be aware that you might encounter errors such as:
- Invalid URL format
- Missing API key
- Rate limit exceeded
- Network or API errors
If you encounter any errors, provide a clear explanation of what went wrong
and suggest possible solutions.
""",
expected_output="Either the extracted heading or a clear error explanation",
agent=web_scraper_agent,
)限流
Scrapegraph API 有速率限制,具体限制因你的订阅计划而异。请考虑以下最佳实践:
- 在处理多个 URL 时,在请求之间实现适当的延迟。
- 在应用中优雅地处理速率限制错误。
- 在 Scrapegraph 仪表板上查看你的 API 计划限制。
实现细节
ScrapegraphScrapeTool 使用 Scrapegraph Python 客户端与 SmartScraper API 交互:
python
class ScrapegraphScrapeTool(BaseTool):
"""
A tool that uses Scrapegraph AI to intelligently scrape website content.
"""
# Implementation details...
def _run(self, **kwargs: Any) -> Any:
website_url = kwargs.get("website_url", self.website_url)
user_prompt = (
kwargs.get("user_prompt", self.user_prompt)
or "Extract the main content of the webpage"
)
if not website_url:
raise ValueError("website_url is required")
# Validate URL format
self._validate_url(website_url)
try:
# Make the SmartScraper request
response = self._client.smartscraper(
website_url=website_url,
user_prompt=user_prompt,
)
return response
# Error handling...结论
ScrapegraphScrapeTool 提供了一种利用 AI 对网页结构的理解从网站中提取内容的强大方式。通过让智能体使用自然语言提示词定位特定信息,它使网页抓取任务更高效、更聚焦。该工具尤其适用于需要从网页中提取特定信息的数据提取、内容监控和研究任务。