网页抓取工具(web fetch tool)允许 Claude 从指定的网页和 PDF 文档中检索完整内容。
最新的网页抓取工具版本(web_fetch_20260318)在 Claude Fable 5、Claude Opus 4.8、Claude Mythos 5、Claude Mythos Preview、Claude Opus 4.7、Claude Opus 4.6 和 Claude Sonnet 4.6 上支持动态过滤。Claude 可以编写并执行代码,在抓取的内容进入上下文窗口之前对其进行过滤,仅保留相关信息并丢弃其余部分。这在保持响应质量的同时减少了令牌消耗。web_fetch_20260318 还为智能体工作流新增了响应包含控制。之前的版本(web_fetch_20260309 支持动态过滤和缓存绕过,web_fetch_20260209 仅支持动态过滤,web_fetch_20250910 支持基础抓取)仍然可用。
对于 Claude Mythos Preview,网页抓取在 Claude API 和 Microsoft Foundry 上可用。目前在 Amazon Bedrock 或 Vertex AI 上不适用于 Mythos Preview。
请使用反馈表单就模型响应质量、API 本身或文档质量提供反馈。
有关零数据保留(Zero Data Retention)资格和 allowed_callers 解决方法,请参阅服务器工具。
在 Claude 同时处理不受信任的输入和敏感数据的环境中启用网页抓取工具会带来数据泄露风险。请仅在受信任的环境中或处理非敏感数据时使用此工具。
为了最大限度地降低数据泄露风险,Claude 不允许动态构建 URL。Claude 只能抓取用户明确提供的 URL,或来自之前网页搜索或网页抓取结果的 URL。但是,使用此工具时仍存在残余风险,应仔细考虑。
如果担心数据泄露,请考虑:
max_uses 参数限制请求数量allowed_domains 参数限制为已知的安全域名有关模型支持情况,请参阅工具参考。
当您将网页抓取工具添加到 API 请求中时:
网页抓取工具目前不支持使用 JavaScript 动态渲染的网站。
当请求指向特定页面或文档时,Claude 会进行抓取:
对于不引用特定页面的常识性或开放式问题,Claude 不会进行抓取。"总结这篇文章:<url>"会触发抓取;"REST API 设计的最佳实践是什么?"则会直接回答。
抓取完整的网页和 PDF 会快速消耗令牌,尤其是当只需要从大型文档中获取特定信息时。使用 web_fetch_20260209 或更高版本时,Claude 可以编写并执行代码,在将抓取的内容加载到上下文之前对其进行过滤。
这种动态过滤特别适用于:
动态过滤需要启用代码执行工具。网页抓取工具(无论是否启用动态过滤)在 Claude API、AWS 上的 Claude Platform 和 Microsoft Foundry 上可用。目前在 Amazon Bedrock 或 Vertex AI 上不可用。
要启用动态过滤,请使用 web_fetch_20260209 或任何更高版本。以下示例使用 web_fetch_20260209:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=4096,
messages=[
{
"role": "user",
"content": "Fetch the content at https://example.com/research-paper and extract the key findings.",
}
],
tools=[{"type": "web_fetch_20260209", "name": "web_fetch"}],
)
print(response)在您的 API 请求中提供网页抓取工具:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Please analyze the content at https://example.com/article",
}
],
tools=[{"type": "web_fetch_20250910", "name": "web_fetch", "max_uses": 5}],
)
print(response)网页抓取工具支持以下参数:
{
"type": "web_fetch_20250910",
"name": "web_fetch",
// Optional: Limit the number of fetches per request
"max_uses": 10,
// Optional: Only fetch from these domains
"allowed_domains": ["example.com", "docs.example.com"],
// Optional: Never fetch from these domains
"blocked_domains": ["private.example.com"],
// Optional: Enable citations for fetched content
"citations": {
"enabled": true
},
// Optional: Maximum content length in tokens
"max_content_tokens": 100000
}max_uses 参数限制执行网页抓取的次数。如果 Claude 尝试的抓取次数超过允许的次数,web_fetch_tool_result 将返回带有 max_uses_exceeded 错误代码的错误。目前没有默认限制。
有关使用 allowed_domains 和 blocked_domains 进行域名过滤的信息,请参阅服务器工具。
max_content_tokens 参数限制包含在上下文中的内容量。如果抓取的内容超过此限制,工具会将其截断。这有助于在抓取大型文档时控制令牌使用量。
max_content_tokens 参数限制是近似值。实际使用的输入令牌数量可能会有少量差异。
需要 web_fetch_20260309 或更高版本(包括 web_fetch_20260318)。
use_cache 参数控制是否可以返回缓存的内容。设置 "use_cache": false 可绕过缓存并抓取最新内容;默认值为 true。仅当用户明确要求获取最新内容或抓取快速变化的来源时才禁用缓存,因为绕过缓存会增加延迟。
需要 web_fetch_20260318 或更高版本。
response_inclusion 参数控制当抓取结果在同一轮次中被已完成的代码执行调用消费时,抓取结果块在 API 响应中的显示方式。设置 "response_inclusion": "excluded" 可从响应中完全删除这些嵌套的 server_tool_use 和结果块对,从而为不需要将原始页面内容回传给客户端的智能体工作流降低输出令牌成本。默认值为 "full"。来自直接调用的结果,或来自在完成前暂停的代码执行调用的结果,始终会完整返回,以便在下一轮次中发回。
{
"tools": [
{
"type": "web_fetch_20260318",
"name": "web_fetch",
"response_inclusion": "excluded"
}
]
}与始终启用引用的网页搜索不同,网页抓取的引用是可选的。设置 "citations": {"enabled": true} 可使 Claude 能够引用抓取文档中的特定段落。
当直接向最终用户显示 API 输出时,必须包含对原始来源的引用。如果您对 API 输出进行了修改,包括在向最终用户显示之前对其进行重新处理和/或与您自己的材料相结合,请在咨询您的法律团队后酌情显示引用。
以下是一个响应结构示例:
{
"role": "assistant",
"content": [
// 1. Claude's decision to fetch
{
"type": "text",
"text": "I'll fetch the content from the article to analyze it."
},
// 2. The fetch request
{
"type": "server_tool_use",
"id": "srvtoolu_01234567890abcdef",
"name": "web_fetch",
"input": {
"url": "https://example.com/article"
}
},
// 3. Fetch results
{
"type": "web_fetch_tool_result",
"tool_use_id": "srvtoolu_01234567890abcdef",
"content": {
"type": "web_fetch_result",
"url": "https://example.com/article",
"content": {
"type": "document",
"source": {
"type": "text",
"media_type": "text/plain",
"data": "Full text content of the article..."
},
"title": "Article Title",
"citations": { "enabled": true }
},
"retrieved_at": "2025-08-25T10:30:00Z"
}
},
// 4. Claude's analysis with citations (if enabled)
{
"text": "Based on the article, ",
"type": "text"
},
{
"text": "the main argument presented is that artificial intelligence will transform healthcare",
"type": "text",
"citations": [
{
"type": "char_location",
"document_index": 0,
"document_title": "Article Title",
"start_char_index": 1234,
"end_char_index": 1456,
"cited_text": "Artificial intelligence is poised to revolutionize healthcare delivery..."
}
]
}
],
"id": "msg_a930390d3a",
"usage": {
"input_tokens": 25039,
"output_tokens": 931,
"server_tool_use": {
"web_fetch_requests": 1
}
},
"stop_reason": "end_turn"
}抓取结果包括:
url:被抓取的 URLcontent:包含抓取内容的文档块retrieved_at:检索内容时的时间戳网页抓取工具会缓存结果以提高性能并减少冗余请求。返回的内容可能并不总是反映该 URL 上可用的最新版本。缓存行为是自动管理的,可能会随时间变化以针对不同的内容类型和使用模式进行优化。
对于 PDF 文档,内容以 base64 编码数据的形式返回:
{
"type": "web_fetch_tool_result",
"tool_use_id": "srvtoolu_02",
"content": {
"type": "web_fetch_result",
"url": "https://example.com/paper.pdf",
"content": {
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": "JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmo..."
},
"citations": { "enabled": true }
},
"retrieved_at": "2025-08-25T10:30:02Z"
}
}当网页抓取工具遇到错误时,Claude API 会返回 200(成功)响应,并在响应正文中表示该错误:
{
"type": "web_fetch_tool_result",
"tool_use_id": "srvtoolu_a93jad",
"content": {
"type": "web_fetch_tool_error",
"error_code": "url_not_accessible"
}
}以下是可能的错误代码:
invalid_input:URL 格式无效url_too_long:URL 超过最大长度(250 个字符)url_not_allowed:URL 被域名过滤规则和模型限制阻止url_not_accessible:无法抓取内容(HTTP 错误)too_many_requests:超出速率限制unsupported_content_type:不支持的内容类型(仅支持文本和 PDF)max_uses_exceeded:超出网页抓取工具的最大使用次数unavailable:发生内部错误出于安全原因,网页抓取工具只能抓取之前在对话上下文中出现过的 URL。这包括:
该工具无法抓取 Claude 生成的任意 URL,也无法抓取来自基于容器的服务器工具(代码执行、Bash 等)的 URL。
网页抓取可与网页搜索无缝配合,实现全面的信息收集:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=4096,
messages=[
{
"role": "user",
"content": "Find recent articles about quantum computing and analyze the most relevant one in detail",
}
],
tools=[
{"type": "web_search_20250305", "name": "web_search", "max_uses": 3},
{
"type": "web_fetch_20250910",
"name": "web_fetch",
"max_uses": 5,
"citations": {"enabled": True},
},
],
)
print(response)在此工作流中,Claude 将:
当同时启用网页搜索和网页抓取工具,且用户指定了特定页面或文档但未提供 URL 时(例如,"阅读 anthropics/anthropic-sdk-python 仓库中的 README"),Claude 会使用网页搜索定位该资源,然后抓取结果。
有关跨轮次缓存工具定义的信息,请参阅工具使用与提示缓存。
启用流式传输后,抓取事件是流的一部分,在内容检索期间会有暂停:
event: message_start
data: {"type": "message_start", "message": {"id": "msg_abc123", "type": "message"}}
event: content_block_start
data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}
// Claude's decision to fetch
event: content_block_start
data: {"type": "content_block_start", "index": 1, "content_block": {"type": "server_tool_use", "id": "srvtoolu_xyz789", "name": "web_fetch"}}
// Fetch URL streamed
event: content_block_delta
data: {"type": "content_block_delta", "index": 1, "delta": {"type": "input_json_delta", "partial_json": "{\"url\":\"https://example.com/article\"}"}}
// Pause while fetch executes
// Fetch results streamed
event: content_block_start
data: {"type": "content_block_start", "index": 2, "content_block": {"type": "web_fetch_tool_result", "tool_use_id": "srvtoolu_xyz789", "content": {"type": "web_fetch_result", "url": "https://example.com/article", "content": {"type": "document", "source": {"type": "text", "media_type": "text/plain", "data": "Article content..."}}}}}
// Claude's response continues...您可以在消息批处理 API 中包含网页抓取工具。通过消息批处理 API 进行的网页抓取工具调用的定价与常规消息 API 请求中的定价相同。
Web fetch(网页抓取)的使用除标准令牌费用外不产生额外费用:
{
"usage": {
"input_tokens": 25039,
"output_tokens": 931,
"cache_read_input_tokens": 0,
"cache_creation_input_tokens": 0,
"server_tool_use": {
"web_fetch_requests": 1
}
}
}Web fetch 工具在 Claude API 上可用,且无需额外付费。您只需为成为对话上下文一部分的抓取内容支付标准令牌费用。
为防止意外抓取会消耗过多令牌的大型内容,请使用 max_content_tokens 参数,根据您的使用场景和预算考量设置适当的限制。
典型内容的令牌使用量示例:
Was this page helpful?