跳转到主要内容
工具调用

网页抓取

抓取 URL 内容作为上下文

涉及数学计算或数据分析的任务,建议同时启用 code_interpreter 和 Web extractor,以提升准确性。

快速开始

通过 Responses API 调用 Web extractor 来摘要网页内容。以下示例使用 web_searchweb_extractor,搭配 qwen3-max-2026-01-23 的思考模式。
  • Python
  • Node.js
  • curl
import os
from openai import OpenAI

client = OpenAI(
  # 如果未配置环境变量,请替换为:api_key="sk-xxx"
  api_key=os.getenv("DASHSCOPE_API_KEY"),
  base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)

response = client.responses.create(
  model="qwen3-max-2026-01-23",
  input="请访问千问云官方文档,找到代码解释器主题并进行总结",
  tools=[
    {"type": "web_search"},
    {"type": "web_extractor"}
  ],
  extra_body={
    "enable_thinking": True
  }
)

# 取消注释可查看中间输出
# print(response.output)
print("=" * 20 + "Response" + "=" * 20)
print(response.output_text)

# 打印工具调用次数
usage = response.usage
print("=" * 20 + "Tool Invocation Count" + "=" * 20)
if hasattr(usage, 'x_tools') and usage.x_tools:
  print(f"Web Extractor invocations: {usage.x_tools.get('web_extractor', {}).get('count', 0)}")

响应结构

响应包含模型生成的文本及工具使用的元数据。
字段说明
output_text模型基于提取的网页内容生成的最终文本响应
output[]中间结果数组,包含 web_extractor_call 对象(每个对象含 goaloutput 字段,分别表示抓取的 URL 和提取的内容)
usage.x_tools.web_extractor.count本次请求中 Web extractor 的调用次数
usage.x_tools.web_search.count本次请求中 Web search 的调用次数

工作原理

  1. 在 API 请求的 tools 数组中添加 web_extractor(通常同时添加 web_search),并在 prompt 中引用 URL 或主题。
  2. 模型自动判断需要抓取的页面,获取页面内容,并将其作为额外的输入 token 追加到上下文中。
  3. 模型基于获取的内容生成响应。
提取的网页内容会增加输入 token 数,影响计费。详见计费

使用场景

场景工具配置原因
回答关于特定 URL 的问题web_extractor(可选搭配 web_search模型抓取并阅读完整页面内容,而非仅搜索摘要
主题研究,搜索多个网页web_search + web_extractorweb_search 查找相关页面;web_extractor 读取完整内容
快速事实查询(无特定 URL)web_search搜索摘要通常足够,更经济高效
当需要模型阅读页面的实际内容(而非搜索结果摘要)时,使用 web_extractor

调用方式

Web extractor 支持三种 API。Responses API 提供最精细的工具控制能力,建议新项目优先使用。
API工具配置是否必须流式备注
Responses API(推荐)tools 中添加 web_searchweb_extractor支持中间工具执行状态
Chat Completions API设置 enable_search: truesearch_strategy: "agent_max"不支持非流式
DashScope API设置 enable_search: truesearch_strategy: "agent_max"不支持 Java SDK
使用 qwen3-max-2026-01-23 时,需将 enable_thinking 设为 true
  • Responses API
  • Chat Completions API
  • DashScope API
response = client.responses.create(
  model="qwen3-max-2026-01-23",
  input="总结 https://example.com/article 的内容",
  tools=[
    {"type": "web_search"},
    {"type": "web_extractor"}
  ],
  extra_body={
    "enable_thinking": True
  }
)

流式输出 Web extractor 事件

流式输出的通用概念(SSE 协议、启用方式、token 用量)请参阅流式输出。本节仅介绍 Web extractor 特有的事件类型。
网页抓取可能需要一定时间。使用流式输出可以实时接收推理步骤、工具调用和响应文本。Responses API 支持暴露每个工具调用的中间执行状态,是流式场景的最佳选择。 使用 Responses API 进行流式输出时,以下事件类型表示提取和生成流程的各个阶段:
事件类型说明
response.reasoning_summary_text.delta模型思考过程的增量推理文本
response.output_item.done工具调用已完成。检查 item.type 是否为 web_extractor_call 以获取提取结果
response.output_text.delta增量响应文本
response.completed响应完成。usage 字段包含工具调用次数
处理流中的 web_extractor_call 事件时,检查事件类型并读取其 goaloutput 字段:
for chunk in stream:
  if chunk.type == 'response.output_item.done':
    if hasattr(chunk, 'item') and chunk.item.type == 'web_extractor_call':
      print(f"已抓取: {chunk.item.goal}")
      print(f"内容: {chunk.item.output}")
  elif chunk.type == 'response.output_text.delta':
    print(chunk.delta, end='', flush=True)
  elif chunk.type == 'response.completed':
    usage = chunk.response.usage
    if hasattr(usage, 'x_tools') and usage.x_tools:
      print(f"Web Extractor 调用次数: {usage.x_tools.get('web_extractor', {}).get('count', 0)}")

支持的模型

模型系列模型 ID
Qwen-Maxqwen3.7-maxqwen3.7-max-2026-05-20qwen3-maxqwen3-max-2026-01-23(思考模式)
Qwen-Plusqwen3.6-plusqwen3.6-plus-2026-04-02qwen3.5-plusqwen3.5-plus-2026-02-15
Qwen-Flashqwen3.5-flashqwen3.5-flash-2026-02-23
Qwen 开源系列qwen3.5-397b-a17bqwen3.5-122b-a10bqwen3.5-27bqwen3.5-35b-a3b

使用限制

  • Web extractor 仅能获取公开可访问的页面。需要登录认证或付费的页面返回空内容。
  • 过大的页面可能在添加到上下文窗口前被截断。
  • 纯 JavaScript 渲染的动态内容可能无法完整获取。
  • 提取的内容作为输入 token 计算,大页面会增加延迟和成本。

错误处理

提取失败时,模型不会抛出错误。响应输出中的 web_extractor_call 项会返回空或部分内容,模型基于已有上下文生成响应。 常见的失败场景:
场景行为
URL 不可达(404、500、DNS 解析失败)提取返回空内容;模型使用其他可用上下文生成响应
页面加载超时返回部分或空内容
非 HTML 内容(PDF、图片)可能无法提取内容;模型回退到其他工具或通用知识
要验证提取是否成功,可检查 response.output 中的 web_extractor_call 项,或查看 usage.x_tools.web_extractor.count 获取成功调用次数。

计费

Web extractor 的费用包含两部分:
组成部分详情
模型费用提取的网页内容追加到 prompt 中,增加输入 token 数,按模型标准 token 价格计费。详见定价
联网搜索费用每 1,000 次调用 4 元。使用网页抓取需同时开启联网搜索,联网搜索会独立计费。
Web extractor 费用限时免费。