主题
Spider 抓取器
SpiderTool旨在使用 Spider 提取并读取指定网站的内容。
SpiderTool
描述
Spider 是最快的 开源抓取器和爬虫,可返回可直接供 LLM 使用的数据。 它可将任何网站转换为纯 HTML、markdown、元数据或文本,同时让你使用 AI 以自定义动作进行爬取。
安装
要使用 SpiderTool,你需要下载 Spider SDK 以及 crewai[tools] SDK:
shell
pip install spider-client 'crewai[tools]'示例
本示例展示如何使用 SpiderTool 让你的智能体能够抓取和爬取网站。 Spider API 返回的数据已经是可直接供 LLM 使用的,因此无需再做任何清理。
python
from crewai_tools import SpiderTool
def main():
spider_tool = SpiderTool()
searcher = Agent(
role="Web Research Expert",
goal="Find related information from specific URL's",
backstory="An expert web researcher that uses the web extremely well",
tools=[spider_tool],
verbose=True,
)
return_metadata = Task(
description="Scrape https://spider.cloud with a limit of 1 and enable metadata",
expected_output="Metadata and 10 word summary of spider.cloud",
agent=searcher
)
crew = Crew(
agents=[searcher],
tasks=[
return_metadata,
],
verbose=2
)
crew.kickoff()
if __name__ == "__main__":
main()参数
| 参数 | 类型 | 描述 |
|---|---|---|
| api_key | string | 指定 Spider API 密钥。如果未指定,则会在环境变量中查找 SPIDER_API_KEY。 |
| params | object | 请求的可选参数。默认为 {"return_format": "markdown"},以针对 LLM 优化内容。 |
| request | string | 要执行的请求类型(http、chrome、smart)。smart 默认使用 HTTP,必要时切换为 JavaScript 渲染。 |
| limit | int | 每个网站爬取的最大页面数。设为 0 或省略表示不限制。 |
| depth | int | 最大爬取深度。设为 0 表示不限制。 |
| cache | bool | 启用 HTTP 缓存以加快重复运行速度。默认值为 true。 |
| budget | object | 为爬取的页面设置基于路径的限制,例如 {"*":1} 表示仅根页面。 |
| locale | string | 请求的区域设置,例如 en-US。 |
| cookies | string | 请求使用的 HTTP cookie。 |
| stealth | bool | 为 Chrome 请求启用隐身模式以避免被检测。默认值为 true。 |
| headers | object | 以键值对映射形式表示的所有请求的 HTTP 头。 |
| metadata | bool | 存储关于页面和内容的元数据,有助于 AI 互操作。默认值为 false。 |
| viewport | object | 设置 Chrome 视口尺寸。默认值为 800x600。 |
| encoding | string | 指定编码类型,例如 UTF-8、SHIFT_JIS。 |
| subdomains | bool | 在爬取中包含子域名。默认值为 false。 |
| user_agent | string | 自定义 HTTP user agent。默认使用随机 agent。 |
| store_data | bool | 为请求启用数据存储。设置后会覆盖 storageless。默认值为 false。 |
| gpt_config | object | 允许 AI 生成爬取动作,可通过 "prompt" 的数组设置可选的链式步骤。 |
| fingerprint | bool | 为 Chrome 启用高级指纹识别。 |
| storageless | bool | 阻止所有数据存储,包括 AI 嵌入。默认值为 false。 |
| readability | bool | 通过 Mozilla 的 readability 对内容进行阅读预处理。为 LLM 优化内容。 |
| return_format | string | 返回数据的格式:markdown、raw、text、html2text。使用 raw 表示默认页面格式。 |
| proxy_enabled | bool | 启用高性能代理以避免网络层面的封锁。 |
| query_selector | string | 用于从标记中提取内容的 CSS 查询选择器。 |
| full_resources | bool | 下载与网站关联的所有资源。 |
| request_timeout | int | 请求超时时间(秒,5-60)。默认值为 30。 |
| run_in_background | bool | 在后台运行请求,适用于数据存储和触发仪表板爬取。如果设置了 storageless 则无效。 |