大模型無法直接擷取網頁資料。網頁抓取工具可以訪問指定 URL 並提取內容,為大模型提供所需資訊。
使用方式
網頁抓取功能支援四種調用方式,啟用參數有所不同:
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 中加入名稱為 web_fetch 的工具,並在同一請求中加入名稱為 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 的日期後綴由用戶端版本決定,服務端只識別工具類別,不驗證日期後綴;請傳入用戶端實際使用的值。只加入抓取工具而未加入搜尋工具時,請求會回傳 InvalidParameter 錯誤:The web_extractor tool must be executed with web_search tool.
kimi-k3.1-pro 也支援此功能。
以下為已建立 Anthropic 用戶端後的呼叫片段;用戶端初始化方式請參見 Anthropic Messages API。呼叫快照版本時,將 model 設為 deepseek-v4.1-flash-1017。
# Import dependencies and create the client...
message = client.messages.create(
model="qwen4-max",
max_tokens=4096,
# Pass the client identifier
system=[{"type": "text", "text": "x-anthropic-billing-header: cc_entrypoint=cli;"}],
messages=[{"role": "user", "content": "Read the Alibaba Cloud Model Studio code interpreter documentation and summarize the key points."}],
tools=[
# Enabling the web extractor requires the web search tool as well
# The date suffix in type depends on the client version. The values below are the ones current Claude Code sends.
{"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。
支援的模型
deepseek-v4.1-flash-1017 支援 Responses API 內建 web_extractor 和 Anthropic 相容 API 內建 web_fetch,適用於模型頁面所列的全部六個地域及對應部署範圍。
kimi-k3.1-pro(阿里雲百鍊部署)在北京、新加坡、美國(維吉尼亞)、德國(法蘭克福)、日本(東京)和中國香港均支援 Responses API 內建 web_extractor 和 Anthropic 相容 API 的 web_fetch。
推薦模型
Responses API
千問Max:qwen4-max、Qwen3.8-Max系列、Qwen3.7-Max系列
千問Plus:Qwen3.7-Plus系列、Qwen3.6-Plus系列、Qwen3.5-Plus系列
DeepSeek:deepseek-v4-flash-0731
Qwen3.8開源系列
其他模型
以下模型也支援此工具調用,但效果不如推薦模型。僅支援通過Responses API調用。
- 千問Flash:Qwen3.7-Flash系列、Qwen3.6-Flash系列、Qwen3.5-Flash系列
- Qwen3.6開源系列(qwen3.6-27b除外)
- Qwen3.5開源系列
快速開始
運行以下代碼,通過 Responses API 呼叫網頁抓取工具,自動總結一篇技術文檔。
import os
from openai import OpenAI
client = OpenAI(
# 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區的配置,調用時請將 {WorkspaceId} 替換為真實的業務空間ID,各地區的配置不同。
base_url="https://{WorkspaceId}.ap-southeast-1.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,
// 以下為新加坡地區的配置,調用時請將 {WorkspaceId} 替換為真實的業務空間ID,各地區的配置不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.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();
# 以下為新加坡地區的URL,調用時請將 {WorkspaceId} 替換為真實的業務空間ID,各地區的URL不同。
curl -X POST https://{WorkspaceId}.ap-southeast-1.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://www.alibabacloud.com/help/zh/model-studio/qwen-code-interpreter) 與 [Assistant API代碼解譯器](https://www.alibabacloud.com/help/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"),
# 以下為新加坡地區的配置,調用時請將 {WorkspaceId} 替換為真實的業務空間ID,各地區的配置不同。
base_url="https://{WorkspaceId}.ap-southeast-1.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_summary_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,
// 以下為新加坡地區的配置,調用時請將 {WorkspaceId} 替換為真實的業務空間ID,各地區的配置不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.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_summary_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();
# 以下為新加坡地區的URL,調用時請將 {WorkspaceId} 替換為真實的業務空間ID,各地區的URL不同。
curl -X POST https://{WorkspaceId}.ap-southeast-1.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(北京)地區:$0.57341。
- 新加坡地區:$10.00。
-
網頁抓取工具限時免費。
-