大模型无法直接获取网页数据。网页抓取工具可以访问指定 URL 并提取内容,为大模型提供所需信息。
使用方式
网页抓取功能支持通过 Responses API 或 Anthropic 兼容 Messages API 调用,两种方式的工具名称和启用参数有所不同:
OpenAI 兼容-Responses API
要启用网页抓取功能,您需要在 tools 参数中同时添加 web_search(联网搜索)和 web_extractor(网页抓取)工具。
为获得最佳回复效果,尤其是在解决数学计算、数据分析类问题时,建议同时开启
code_interpreter工具。这将允许模型在需要时调用代码解释器,提高结果的准确性。
# 导入依赖与创建客户端...
response = client.responses.create(
model="qwen4-max",
input="请访问阿里云百炼代码解释器部分的官方文档,并总结主要内容",
tools=[
# 开启网页抓取必须同时开启联网搜索工具
{"type": "web_search"},
{"type": "web_extractor"},
{"type": "code_interpreter"}
],
extra_body={
# 必须开启思考模式
"enable_thinking": True
}
)
print(response.output_text)
Anthropic 兼容
在 tools 参数中添加 Anthropic 服务端的网页抓取工具(name 为 web_fetch)即可启用网页抓取,并且必须同时添加联网搜索工具(name 为 web_search)。请求地址为 https://{WorkspaceId}.{region}.maas.aliyuncs.com/apps/anthropic。
该方式面向 Claude Code 等 Anthropic 官方客户端。在客户端中直接用自然语言提问即可触发网页抓取,无需额外配置。
使用 Anthropic SDK 或 HTTP 直接调用时,需在
system中传入客户端标识x-anthropic-billing-header: cc_entrypoint=cli;,否则网页抓取不生效。
关于工具版本号:
type中的日期后缀(例如web_fetch_20250910、web_search_20250305)由 Anthropic 官方客户端的版本决定,客户端升级后可能改用其他日期的版本号。百炼按工具类别识别这两个工具,不校验具体的日期后缀,因此无需将type固定为某一版本,与客户端实际发送的值保持一致即可。
只添加网页抓取工具而未添加联网搜索工具时,请求返回
InvalidParameter错误:The web_extractor tool must be executed with web_search tool.
# 导入依赖与创建客户端...
message = client.messages.create(
model="qwen4-max",
max_tokens=4096,
# 传入客户端标识
system=[{"type": "text", "text": "x-anthropic-billing-header: cc_entrypoint=cli;"}],
messages=[{"role": "user", "content": "请访问阿里云百炼代码解释器部分的官方文档,并总结主要内容"}],
tools=[
# 开启网页抓取必须同时开启联网搜索工具
# type 中的日期后缀由客户端版本决定,此处以 Claude Code 当前使用的版本为例
{"type": "web_search_20250305", "name": "web_search", "max_uses": 5},
{"type": "web_fetch_20250910", "name": "web_fetch", "max_uses": 5}
]
)
抓取过程通过 server_tool_use(抓取请求)和 web_fetch_tool_result(抓取结果)内容块返回,抓取次数记录在 usage.server_tool_use.web_fetch_requests 中。
支持的模型
- 千问 Max:Qwen4-Max 系列、Qwen3.8-Max 系列、Qwen3.7-Max 系列。
- 千问 Plus:Qwen3.7-Plus 系列、Qwen3.6-Plus 系列、Qwen3.5-Plus 系列。
- 千问 Flash:Qwen3.8-Flash 系列、Qwen3.7-Flash 系列、Qwen3.6-Flash 系列、Qwen3.5-Flash 系列。
- 千问开源模型:Qwen4 开源系列、Qwen3.8 开源系列、Qwen3.6 开源系列(不含
qwen3.6-27b)、Qwen3.5 开源系列。 - DeepSeek:
deepseek-v4-pro、deepseek-v4-pro-0813、deepseek-v4-flash-0731、deepseek-v4.1-flash-1017。 - Kimi:
kimi-k3、kimi-k3.1-pro(阿里云百炼部署)。 - GLM:
glm-5.2。
地域与部署范围请参见 Qwen4-35B-A3B、DeepSeek-V4.1-Flash 等模型详情页。kimi-k3.1-pro 支持北京、新加坡、美国(弗吉尼亚)、德国(法兰克福)、日本(东京)和中国香港。
快速开始
运行以下代码,通过 Responses API 调用网页抓取工具,自动总结一篇技术文档。
import os
from openai import OpenAI
client = OpenAI(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx"(不建议),
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下为华北2(北京)地域的配置,调用时请将 {WorkspaceId} 替换为真实的业务空间ID,各地域的配置不同。
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"
)
response = client.responses.create(
model="qwen4-max",
input="请访问阿里云百炼代码解释器部分的官方文档,并总结主要内容",
tools=[
{
"type": "web_search"
},
{
"type": "web_extractor"
},
{
"type": "code_interpreter"
}
],
extra_body = {
"enable_thinking": True
}
)
# 取消以下注释查看中间过程输出
# print(response.output)
print("="*20+"回复内容"+"="*20)
print(response.output_text)
# 打印工具调用次数
usage = getattr(response, "tool_usage", {})
print("="*20+"工具调用次数"+"="*20)
if usage:
print(f"\n网页抓取运行次数: {usage.get('web_extractor', {}).get('num_requests')}")
import OpenAI from "openai";
import process from 'process';
const openai = new OpenAI({
// 若没有配置环境变量,请用百炼API Key将下行替换为:apiKey: "sk-xxx",
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下为华北2(北京)地域的配置,调用时请将 {WorkspaceId} 替换为真实的业务空间ID,各地域的配置不同。
baseURL: "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"
});
async function main() {
const response = await openai.responses.create({
model: "qwen4-max",
input: "请访问阿里云百炼代码解释器部分的官方文档,并总结主要内容",
tools: [
{ type: "web_search" },
{ type: "web_extractor" },
{ type: "code_interpreter" }
],
enable_thinking: true
});
console.log("====================回复内容====================");
console.log(response.output_text);
// 打印工具调用次数
console.log("====================工具调用次数====================");
if (response.tool_usage) {
console.log(`网页抓取次数: ${response.tool_usage.web_extractor?.num_requests}`);
console.log(`联网搜索次数: ${response.tool_usage.web_search?.num_requests}`);
}
// 取消以下注释查看中间过程的输出
// console.log(JSON.stringify(response.output[0], null, 2));
}
main();
# 以下为华北2(北京)地域的URL,调用时请将 {WorkspaceId} 替换为真实的业务空间ID,各地域的URL不同。
curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen4-max",
"input": "请访问阿里云百炼代码解释器部分的官方文档,并总结主要内容",
"tools": [
{"type": "web_search"},
{"type": "web_extractor"},
{"type": "code_interpreter"}
],
"enable_thinking": true
}'
运行以上代码可获取如下回复:
====================回复内容====================
根据阿里云百炼官方文档,我为您总结了**代码解释器**功能的核心内容:
## 一、功能定位
...
> **文档来源**:阿里云百炼官方文档 - [Qwen代码解释器](https://help.aliyun.com/zh/model-studio/qwen-code-interpreter) 与 [Assistant API代码解释器](https://help.aliyun.com/zh/model-studio/code-interpreter)(更新时间:2025年12月)
====================工具调用次数====================
网页抓取运行次数: 1
流式输出
网页抓取耗时较长,建议启用流式输出,实时获取中间过程输出结果。
建议优先使用Responses API,以获取工具的中间执行状态。
OpenAI 兼容-Responses API
import os
from openai import OpenAI
client = OpenAI(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下为华北2(北京)地域的配置,调用时请将 {WorkspaceId} 替换为真实的业务空间ID,各地域的配置不同。
base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"
)
stream = client.responses.create(
model="qwen4-max",
input="请访问阿里云百炼代码解释器部分的官方文档,并总结主要内容",
tools=[
{"type": "web_search"},
{"type": "web_extractor"},
{"type": "code_interpreter"}
],
stream=True,
extra_body={"enable_thinking": True}
)
reasoning_started = False
output_started = False
for chunk in stream:
# 打印思考过程
if chunk.type == 'response.reasoning_text.delta':
if not reasoning_started:
print("="*20 + "思考过程" + "="*20)
reasoning_started = True
print(chunk.delta, end='', flush=True)
# 打印工具调用完成
elif chunk.type == 'response.output_item.done':
if hasattr(chunk, 'item') and hasattr(chunk.item, 'type'):
if chunk.item.type == 'web_extractor_call':
print("\n" + "="*20 + "工具调用" + "="*20)
print(chunk.item.goal)
print(chunk.item.output)
elif chunk.item.type == 'reasoning':
reasoning_started = False
# 打印回复内容
elif chunk.type == 'response.output_text.delta':
if not output_started:
print("\n" + "="*20 + "回复内容" + "="*20)
output_started = True
print(chunk.delta, end='', flush=True)
# 响应完成,打印工具调用次数
elif chunk.type == 'response.completed':
print("\n" + "="*20 + "工具调用次数" + "="*20)
usage = getattr(chunk.response, "tool_usage", {})
if usage:
print(f"网页抓取次数: {usage.get('web_extractor', {}).get('num_requests')}")
print(f"联网搜索次数: {usage.get('web_search', {}).get('num_requests')}")
import OpenAI from "openai";
import process from 'process';
const openai = new OpenAI({
// 若没有配置环境变量,请用百炼API Key将下行替换为:apiKey: "sk-xxx",
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下为华北2(北京)地域的配置,调用时请将 {WorkspaceId} 替换为真实的业务空间ID,各地域的配置不同。
baseURL: "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1"
});
async function main() {
const stream = await openai.responses.create({
model: "qwen4-max",
input: "请访问阿里云百炼代码解释器部分的官方文档,并总结主要内容",
tools: [
{ type: "web_search" },
{ type: "web_extractor" },
{ type: "code_interpreter" }
],
stream: true,
enable_thinking: true
});
let reasoningStarted = false;
let outputStarted = false;
for await (const chunk of stream) {
// 打印思考过程
if (chunk.type === 'response.reasoning_text.delta') {
if (!reasoningStarted) {
console.log("====================思考过程====================");
reasoningStarted = true;
}
process.stdout.write(chunk.delta);
}
// 打印工具调用完成
else if (chunk.type === 'response.output_item.done') {
if (chunk.item && chunk.item.type === 'web_extractor_call') {
console.log("\n" + "====================工具调用====================");
console.log(chunk.item.goal);
console.log(chunk.item.output);
} else if (chunk.item && chunk.item.type === 'reasoning') {
reasoningStarted = false;
}
}
// 打印回复内容
else if (chunk.type === 'response.output_text.delta') {
if (!outputStarted) {
console.log("\n" + "====================回复内容====================");
outputStarted = true;
}
process.stdout.write(chunk.delta);
}
// 响应完成,打印工具调用次数
else if (chunk.type === 'response.completed') {
console.log("\n" + "====================工具调用次数====================");
const usage = chunk.response.tool_usage;
if (usage) {
console.log(`网页抓取次数: ${usage.web_extractor?.num_requests}`);
console.log(`联网搜索次数: ${usage.web_search?.num_requests}`);
}
}
}
}
main();
# 以下为华北2(北京)地域的URL,调用时请将 {WorkspaceId} 替换为真实的业务空间ID,各地域的URL不同。
curl -X POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen4-max",
"input": "请访问阿里云百炼代码解释器部分的官方文档,并总结主要内容",
"tools": [
{"type": "web_search"},
{"type": "web_extractor"},
{"type": "code_interpreter"}
],
"enable_thinking": true,
"stream": true
}'
计费说明
计费涉及以下方面:
-
模型调用费用:抓取的网页内容会拼接到提示词中,增加模型的输入 Token,按照模型的标准价格计费。价格详情请参考百炼控制台。
-
工具调用费用:包含网页抓取与联网搜索的费用。
-
联网搜索工具每 1000 次调用费用:
- 华北2(北京)、美国(弗吉尼亚)、中国香港、日本(东京)、德国(法兰克福)地域:4元。
- 新加坡地域: 73.392381元。
-
网页抓取工具限时免费。
-