网页抓取 - 千问云
跳转到主要内容
工具调用

网页抓取

抓取 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/api/v2/apps/protocols/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
  }
)

函数调用的唯一标识符。仅当 type 为 function_call 时存在此字段。返回函数调用结果时,必须使用此 ID 关联请求与响应。

"qwen3-max-2026-01-23", messages=[{"role": "user", "content": "总结 https://example.com/article 的内容"}], extra_body={ "enable_thinking": True, "enable_search": True, "search_options": {"search_strategy": "agent_max"} }, stream=True )

消息内容数组。仅当 type 为 message 时存在此字段。

文本注释数组。通常为空数组。

object[]

事件类型说明
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':
推理摘要数组。仅当 type 为 reasoning 时存在此字段。每个元素包含 type 字段(值为 summary_text)和 text 字段(包含摘要文本)。

支持的模型

模型系列模型 ID
Qwen-Maxqwen3-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

object

搜索动作信息。仅当 type 为 web_search_call 时存在此字段。

string

模型生成并执行的代码。仅当 type 为 code_interpreter_call 时存在此字段。

object[]

代码执行输出数组。仅当 type 为 code_interpreter_call 时存在此字段。每个元素包含 type 字段(值为 logs)和 logs 字段(包含代码执行日志)。

string

代码解释器容器的标识符。仅当 type 为 code_interpreter_call 时存在此字段。用于在同一会话中关联多次代码执行。

  • 提取目标的描述,说明需要从网页中提取哪些信息。仅当 type 为 web_extractor_call 时存在此字段。

    string

    工具调用的输出结果,为字符串格式。

    • 当 type 为 web_extractor_call 时,为提取的网页内容摘要。
    • 当 type 为 web_search_image_callimage_search_call 时,为包含图片搜索结果数组的 JSON 字符串。每个元素包含 title 字段(图片标题)、url 字段(图片 URL)和 index 字段(序号)。
    • 当 type 为 mcp_call 时,为 MCP 服务返回的 JSON 字符串结果。

    已抓取网页的 URL 列表。仅当 type 为 web_extractor_call 时存在此字段。

    string[]

    用于知识库检索的查询列表。仅当 type 为 file_search_call 时存在此字段。数组元素为模型生成的搜索查询字符串。

    思考模式
  • object[]

    知识库检索结果数组。仅当 type 为 file_search_call 时存在此字段。

    集成
    模型生产