Scrapfly 抓取网站工具
ScrapflyScrapeWebsiteTool
Section titled “ScrapflyScrapeWebsiteTool”ScrapflyScrapeWebsiteTool 旨在利用 Scrapfly 的网页抓取 API 从网站中提取内容。该工具提供了高级网页抓取能力,支持无头浏览器、代理和反机器人绕过功能。它可以提取多种格式的网页数据,包括原始 HTML、markdown 和纯文本,因此适用于各种网页抓取任务。
要使用此工具,你需要安装 Scrapfly SDK:
uv add scrapfly-sdk你还需要通过在 scrapfly.io/register 注册来获取 Scrapfly API key。
要有效使用 ScrapflyScrapeWebsiteTool,请按以下步骤进行:
- 安装依赖:使用上面的命令安装 Scrapfly SDK。
- 获取 API Key:在 Scrapfly 注册并获取你的 API key。
- 初始化工具:使用你的 API key 创建工具实例。
- 配置抓取参数:根据需要自定义抓取参数。
下面示例演示如何使用 ScrapflyScrapeWebsiteTool 从网站中提取内容:
from crewai import Agent, Task, Crewfrom crewai_tools import ScrapflyScrapeWebsiteTool
# 初始化工具scrape_tool = ScrapflyScrapeWebsiteTool(api_key="your_scrapfly_api_key")
# 定义一个使用该工具的智能体web_scraper_agent = Agent( role="Web Scraper", goal="从网站中提取信息", backstory="擅长从任何网站提取内容的网页抓取专家。", tools=[scrape_tool], verbose=True,)
# 示例任务:从网站提取内容scrape_task = Task( description="从 https://web-scraping.dev/products 的产品页面提取主要内容,并总结可用产品。", expected_output="网站上可用产品的摘要。", agent=web_scraper_agent,)
# 创建并运行 crewcrew = Crew(agents=[web_scraper_agent], tasks=[scrape_task])result = crew.kickoff()你也可以自定义抓取参数:
# 自定义抓取参数示例web_scraper_agent = Agent( role="Web Scraper", goal="使用自定义参数从网站中提取信息", backstory="擅长从任何网站提取内容的网页抓取专家。", tools=[scrape_tool], verbose=True,)
# 智能体将使用如下参数:# url="https://web-scraping.dev/products"# scrape_format="markdown"# ignore_scrape_failures=True# scrape_config={# "asp": True, # 绕过抓取阻止方案,例如 Cloudflare# "render_js": True, # 使用云端无头浏览器启用 JavaScript 渲染# "proxy_pool": "public_residential_pool", # 选择代理池# "country": "us", # 选择代理位置# "auto_scroll": True, # 自动滚动页面# }
scrape_task = Task( description="使用高级抓取选项(包括 JavaScript 渲染和代理设置)从 https://web-scraping.dev/products 的产品页面提取主要内容。", expected_output="包含所有可用信息的详细产品摘要。", agent=web_scraper_agent,)ScrapflyScrapeWebsiteTool 接受以下参数:
- api_key:必需。你的 Scrapfly API key。
- url:必需。要抓取的网站 URL。
- scrape_format:可选。提取网页内容的格式。可选项为 “raw”(HTML)、“markdown” 或 “text”。默认值为 “markdown”。
- scrape_config:可选。包含其他 Scrapfly 抓取配置选项的字典。
- ignore_scrape_failures:可选。是否忽略抓取过程中的失败。如果设为
True,抓取失败时工具会返回None,而不是抛出异常。
Scrapfly 配置选项
Section titled “Scrapfly 配置选项”scrape_config 参数允许你使用以下选项自定义抓取行为:
- asp:启用反抓取保护绕过。
- render_js:使用云端无头浏览器启用 JavaScript 渲染。
- proxy_pool:选择代理池(例如 “public_residential_pool”、“datacenter”)。
- country:选择代理位置(例如 “us”、“uk”)。
- auto_scroll:自动滚动页面以加载懒加载内容。
- js:通过无头浏览器执行自定义 JavaScript 代码。
完整配置选项请参阅 Scrapfly API 文档。
当你与智能体一起使用 ScrapflyScrapeWebsiteTool 时,智能体需要提供要抓取的网站 URL,并且可以可选地指定格式和附加配置选项:
# 使用该工具与智能体配合的示例web_scraper_agent = Agent( role="Web Scraper", goal="从网站中提取信息", backstory="擅长从任何网站提取内容的网页抓取专家。", tools=[scrape_tool], verbose=True,)
# 为智能体创建任务scrape_task = Task( description="以 markdown 格式提取 example.com 的主要内容。", expected_output="example.com 的 markdown 格式主要内容。", agent=web_scraper_agent,)
# 运行任务crew = Crew(agents=[web_scraper_agent], tasks=[scrape_task])result = crew.kickoff()对于带自定义配置的更高级用法:
# 创建一个更具体的任务advanced_scrape_task = Task( description=""" 按以下要求从 example.com 提取内容: - 将内容转换为纯文本格式 - 启用 JavaScript 渲染 - 使用美国代理 - 优雅地处理任何抓取失败 """, expected_output="从 example.com 提取的内容", agent=web_scraper_agent,)默认情况下,如果抓取失败,ScrapflyScrapeWebsiteTool 会抛出异常。你可以通过指定 ignore_scrape_failures 参数,指示智能体优雅地处理失败:
# 创建一个指示智能体处理错误的任务error_handling_task = Task( description=""" 从一个可能有问题的网站提取内容,并确保将任何 抓取失败都通过将 ignore_scrape_failures 设为 True 来优雅处理。 """, expected_output="提取到的内容或一个优雅的错误消息", agent=web_scraper_agent,)ScrapflyScrapeWebsiteTool 使用 Scrapfly SDK 与 Scrapfly API 交互:
class ScrapflyScrapeWebsiteTool(BaseTool): name: str = "Scrapfly web scraping API tool" description: str = ( "Scrape a webpage url using Scrapfly and return its content as markdown or text" )
# Implementation details...
def _run( self, url: str, scrape_format: str = "markdown", scrape_config: Optional[Dict[str, Any]] = None, ignore_scrape_failures: Optional[bool] = None, ): from scrapfly import ScrapeApiResponse, ScrapeConfig
scrape_config = scrape_config if scrape_config is not None else {} try: response: ScrapeApiResponse = self.scrapfly.scrape( ScrapeConfig(url, format=scrape_format, **scrape_config) ) return response.scrape_result["content"] except Exception as e: if ignore_scrape_failures: logger.error(f"Error fetching data from {url}, exception: {e}") return None else: raise eScrapflyScrapeWebsiteTool 提供了一种利用 Scrapfly 高级网页抓取能力从网站中提取内容的强大方式。凭借无头浏览器支持、代理和反机器人绕过等功能,它可以处理复杂网站并以多种格式提取内容。对于需要可靠网页抓取的数据提取、内容监控和研究任务,这个工具非常有用。