主题
从网站抓取元素工具
ScrapeElementFromWebsiteTool让 CrewAI 智能体能够使用 CSS 选择器从网站中提取特定元素。
ScrapeElementFromWebsiteTool
描述
ScrapeElementFromWebsiteTool 旨在使用 CSS 选择器从网站中提取特定元素。该工具让 CrewAI 智能体能够从网页中抓取目标内容,因此适用于只需要网页中特定部分的数据提取任务。抓取请求会经过 CrewAI 的 SSRF 安全 HTTP 辅助程序:请求的 URL 以及每一次重定向跳转都会对照私有地址与保留地址范围(包括云元数据)进行检查,并且 TCP 连接会被固定到通过该检查的 IP 上。
安装
要使用此工具,你需要安装所需的依赖项:
shell
uv add requests beautifulsoup4入门步骤
要有效使用 ScrapeElementFromWebsiteTool,请按以下步骤操作:
- 安装依赖项:使用上面的命令安装所需的包。
- 确定 CSS 选择器:确定你要从网站中提取的元素所对应的 CSS 选择器。
- 初始化工具:使用必要的参数创建该工具的实例。
示例
以下示例演示如何使用 ScrapeElementFromWebsiteTool 从网站中提取特定元素:
python
from crewai import Agent, Task, Crew
from crewai_tools import ScrapeElementFromWebsiteTool
# Initialize the tool
scrape_tool = ScrapeElementFromWebsiteTool()
# 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 headlines from a news website
scrape_task = Task(
description="Extract the main headlines from the CNN homepage. Use the CSS selector '.headline' to target the headline elements.",
expected_output="A list of the main headlines from CNN.",
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 = ScrapeElementFromWebsiteTool(
website_url="https://www.example.com",
css_element=".main-content"
)参数
ScrapeElementFromWebsiteTool 在初始化时接受以下参数:
- website_url:可选。要抓取的网站 URL。如果在初始化时提供,智能体在使用该工具时就无需再指定它。
- css_element:可选。用于提取元素的 CSS 选择器。如果在初始化时提供,智能体在使用该工具时就无需再指定它。
- cookies:可选。一个包含随请求发送的 cookie 的字典。这对于需要身份认证的网站很有用。
用量
在与智能体一起使用 ScrapeElementFromWebsiteTool 时,智能体需要提供以下参数(除非这些参数已在初始化时指定):
- website_url:要抓取的网站 URL。
- css_element:用于提取元素的 CSS 选择器。
该工具将返回所有匹配该 CSS 选择器的元素的文本内容,并以换行符连接。
python
# Example of using the tool with an agent
web_scraper_agent = Agent(
role="Web Scraper",
goal="Extract specific elements from websites",
backstory="An expert in web scraping who can extract targeted content using CSS selectors.",
tools=[scrape_tool],
verbose=True,
)
# Create a task for the agent to extract specific elements
extract_task = Task(
description="""
Extract all product titles from the featured products section on example.com.
Use the CSS selector '.product-title' to target the title elements.
""",
expected_output="A list of product titles from the website",
agent=web_scraper_agent,
)
# Run the task through a crew
crew = Crew(agents=[web_scraper_agent], tasks=[extract_task])
result = crew.kickoff()实现细节
ScrapeElementFromWebsiteTool 使用 requests 库抓取网页,并使用 BeautifulSoup 解析 HTML 并提取指定的元素:
python
class ScrapeElementFromWebsiteTool(BaseTool):
name: str = "Read a website content"
description: str = "A tool that can be used to read a website content."
# Implementation details...
def _run(self, **kwargs: Any) -> Any:
website_url = kwargs.get("website_url", self.website_url)
css_element = kwargs.get("css_element", self.css_element)
page = requests.get(
website_url,
headers=self.headers,
cookies=self.cookies if self.cookies else {},
)
parsed = BeautifulSoup(page.content, "html.parser")
elements = parsed.select(css_element)
return "\n".join([element.get_text() for element in elements])结论
ScrapeElementFromWebsiteTool 提供了一种使用 CSS 选择器从网站中提取特定元素的强大方式。通过让智能体只定位它们所需的内容,它使网页抓取任务更高效、更聚焦。该工具尤其适用于需要从网页中提取特定信息的数据提取、内容监控和研究任务。