Loading...
    • 构建
    • 管理
    • 模型和定价
    • 客户端 SDK
    • API 参考
    Search...
    ⌘K
    第一步
    Claude 简介快速开始
    使用 Claude 构建
    功能概览使用 Messages APIClaude API 技能处理停止原因
    模型能力
    扩展思考自适应思考工作量任务预算(测试版)快速模式(测试版:研究预览)结构化输出引用流式消息批量处理搜索结果流式拒绝多语言支持嵌入
    工具
    概览工具使用原理网络搜索工具网络获取工具代码执行工具顾问工具内存工具Bash 工具计算机使用工具文本编辑器工具
    工具基础设施
    工具参考工具搜索程序化工具调用细粒度工具流式传输
    上下文管理
    上下文窗口压缩上下文编辑提示缓存Token 计数
    处理文件
    Files APIPDF 支持图像和视觉
    技能
    概览快速开始最佳实践企业技能API 中的技能
    MCP
    远程 MCP 服务器MCP 连接器
    提示工程
    概览提示最佳实践Console 提示工具
    测试和评估
    定义成功并构建评估在 Console 中使用评估工具降低延迟
    加强防护栏
    减少幻觉提高输出一致性缓解越狱减少提示泄露
    资源
    术语表
    发布说明
    Claude Platform
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    工具

    网页获取工具

    从特定URL获取并读取内容,以实时网页内容增强Claude的上下文。

    网页获取工具允许Claude从指定的网页和PDF文档中检索完整内容。

    最新的网页获取工具版本(web_fetch_20260209)支持与Claude Mythos Preview、Claude Opus 4.7、Claude Opus 4.6和Claude Sonnet 4.6配合使用的动态过滤。Claude可以编写并执行代码来过滤获取的内容,然后再将其加载到上下文窗口中,仅保留相关信息并丢弃其余部分。这可以减少令牌消耗,同时保持响应质量。之前的工具版本(web_fetch_20250910)仍然可用,但不支持动态过滤。

    对于Claude Mythos Preview,网页获取仅在Claude API和Microsoft Foundry上受支持。在Amazon Bedrock或Google Vertex AI上的Mythos Preview不支持此功能。

    使用反馈表单来提供关于模型响应质量、API本身或文档质量的反馈。

    有关零数据保留资格和allowed_callers解决方案,请参阅服务器工具。

    在Claude处理不受信任的输入与敏感数据的环境中启用网页获取工具会带来数据泄露风险。仅在受信任的环境中或处理非敏感数据时使用此工具。

    为了最小化泄露风险,Claude不允许动态构造URL。Claude只能获取用户明确提供的URL或来自之前网页搜索或网页获取结果的URL。但是,仍然存在使用此工具时应仔细考虑的残留风险。

    如果数据泄露是一个问题,请考虑:

    • 完全禁用网页获取工具
    • 使用max_uses参数限制请求数量
    • 使用allowed_domains参数限制为已知的安全域名

    有关模型支持,请参阅工具参考。

    网页获取工作原理

    当您将网页获取工具添加到API请求时:

    1. Claude根据提示和可用URL决定何时获取内容。
    2. API从指定的URL检索完整文本内容。
    3. 对于PDF,执行自动文本提取。
    4. Claude分析获取的内容并提供带有可选引用的响应。

    网页获取工具目前不支持通过JavaScript动态呈现的网站。

    动态过滤

    获取完整网页和PDF可能会快速消耗令牌,特别是当只需要从大型文档中获取特定信息时。使用web_fetch_20260209工具版本,Claude可以编写并执行代码来过滤获取的内容,然后再将其加载到上下文中。

    动态过滤特别适用于:

    • 从长文档中提取特定部分
    • 处理网页中的结构化数据
    • 从PDF中过滤相关信息
    • 在处理大型文档时降低令牌成本

    动态过滤需要启用代码执行工具。网页获取工具(带有和不带有动态过滤)在Claude API和Microsoft Azure上可用。

    要启用动态过滤,请使用web_fetch_20260209工具版本:

    如何使用网页获取

    在API请求中提供网页获取工具:

    工具定义

    网页获取工具支持以下参数:

    JSON
    {
      "type": "web_fetch_20250910",
      "name": "web_fetch",
    
      // 可选:限制每个请求的获取次数
      "max_uses": 10,
    
      // 可选:仅从这些域名获取
      "allowed_domains": ["example.com", "docs.example.com"],
    
      // 可选:从不从这些域名获取
      "blocked_domains": ["private.example.com"],
    
      // 可选:为获取的内容启用引用
      "citations": {
        "enabled": true
      },
    
      // 可选:最大内容长度(令牌)
      "max_content_tokens": 100000
    }

    最大使用次数

    max_uses参数限制执行的网页获取次数。如果Claude尝试的获取次数超过允许的次数,web_fetch_tool_result将是一个带有max_uses_exceeded错误代码的错误。目前没有默认限制。

    域名过滤

    有关使用allowed_domains和blocked_domains的域名过滤,请参阅服务器工具。

    内容限制

    max_content_tokens参数限制包含在上下文中的内容量。如果获取的内容超过此限制,工具将截断它。这有助于在获取大型文档时控制令牌使用。

    max_content_tokens参数限制是近似的。实际使用的输入令牌数量可能会有少量变化。

    引用

    与网页搜索不同(引用始终启用),网页获取的引用是可选的。设置"citations": {"enabled": true}以启用Claude引用获取文档中的特定段落。

    在直接向最终用户显示API输出时,必须包含对原始来源的引用。如果您在显示给最终用户之前对API输出进行修改,包括重新处理和/或将其与您自己的材料结合,请根据与您的法律团队的协商适当显示引用。

    响应

    以下是一个示例响应结构:

    Output
    {
      "role": "assistant",
      "content": [
        // 1. Claude的获取决定
        {
          "type": "text",
          "text": "I'll fetch the content from the article to analyze it."
        },
        // 2. 获取请求
        {
          "type": "server_tool_use",
          "id": "srvtoolu_01234567890abcdef",
          "name": "web_fetch",
          "input": {
            "url": "https://example.com/article"
          }
        },
        // 3. 获取结果
        {
          "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的分析与引用(如果启用)
        {
          "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:获取的URL
    • content:包含获取内容的文档块
    • retrieved_at:检索内容的时间戳

    网页获取工具缓存结果以提高性能并减少冗余请求。返回的内容可能不总是反映URL上可用的最新版本。缓存行为由系统自动管理,可能会随时间变化以优化不同的内容类型和使用模式。

    对于PDF文档,内容以base64编码数据的形式返回:

    Output
    {
      "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(成功)响应,错误在响应体中表示:

    Output
    {
      "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验证

    出于安全原因,网页获取工具只能获取之前在对话上下文中出现过的URL。这包括:

    • 用户消息中的URL
    • 客户端工具结果中的URL
    • 来自之前网页搜索或网页获取结果的URL

    该工具无法获取Claude生成的任意URL或来自基于容器的服务器工具(代码执行、Bash等)的URL。

    组合搜索和获取

    网页获取与网页搜索无缝配合,用于全面的信息收集:

    Python
    client = anthropic.Anthropic()
    
    response = client.messages.create(
        model="claude-opus-4-7",
        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},
            },
        ],
    )

    在此工作流中,Claude将:

    1. 使用网页搜索查找相关文章
    2. 选择最有前景的结果
    3. 使用网页获取检索完整内容
    4. 提供带有引用的详细分析

    提示缓存

    有关在多轮对话中缓存工具定义,请参阅使用提示缓存的工具使用。

    流式传输

    启用流式传输后,获取事件是流的一部分,在内容检索期间暂停:

    Output
    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的获取决定
    
    event: content_block_start
    data: {"type": "content_block_start", "index": 1, "content_block": {"type": "server_tool_use", "id": "srvtoolu_xyz789", "name": "web_fetch"}}
    
    // 获取URL流式传输
    event: content_block_delta
    data: {"type": "content_block_delta", "index": 1, "delta": {"type": "input_json_delta", "partial_json": "{\"url\":\"https://example.com/article\"}"}}
    
    // 在获取执行期间暂停
    
    // 获取结果流式传输
    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的响应继续...

    批量请求

    您可以在消息批处理API中包含网页获取工具。通过消息批处理API的网页获取工具调用的定价与常规消息API请求中的相同。

    使用和定价

    Web fetch usage has no additional charges beyond standard token costs:

    "usage": {
      "input_tokens": 25039,
      "output_tokens": 931,
      "cache_read_input_tokens": 0,
      "cache_creation_input_tokens": 0,
      "server_tool_use": {
        "web_fetch_requests": 1
      }
    }

    The web fetch tool is available on the Claude API at no additional cost. You only pay standard token costs for the fetched content that becomes part of your conversation context.

    To protect against inadvertently fetching large content that would consume excessive tokens, use the max_content_tokens parameter to set appropriate limits based on your use case and budget considerations.

    Example token usage for typical content:

    • Average web page (10 kB): ~2,500 tokens
    • Large documentation page (100 kB): ~25,000 tokens
    • Research paper PDF (500 kB): ~125,000 tokens

    后续步骤

    服务器工具

    Anthropic执行工具的共享机制。

    工具参考

    所有Anthropic提供的工具的目录。

    Was this page helpful?

    • URL验证
    client = anthropic.Anthropic()
    
    response = client.messages.create(
        model="claude-opus-4-7",
        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)
    client = anthropic.Anthropic()
    
    response = client.messages.create(
        model="claude-opus-4-7",
        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)