大模型無法直接擷取網頁資料。網頁抓取工具可以訪問指定 URL 並提取內容,為大模型提供所需資訊。
使用方式
網頁抓取功能支援三種調用方式,啟用參數有所不同:
OpenAI 相容-Responses API
要啟用網頁抓取功能,您需要在 tools 參數中同時添加 web_search(連網搜尋)和 web_extractor(網頁抓取)工具。
當使用 qwen3-max-2026-01-23 時,需要啟用 enable_thinking 參數以開啟思考模式。
為獲得最佳回複效果,尤其是在解決數學計算、資料分析類問題時,建議同時開啟 code_interpreter 工具。這將允許模型在需要時調用代碼解譯器,提高結果的準確性。# 匯入依賴與建立用戶端...
response = client.responses.create(
model="qwen3.7-max",
input="請訪問阿里雲百鍊代碼解譯器部分的官方文檔,並總結主要內容",
tools=[
# 開啟網頁抓取必須同時開啟連網搜尋工具
{"type": "web_search"},
{"type": "web_extractor"},
{"type": "code_interpreter"}
],
extra_body={
# 必須開啟思考模式
"enable_thinking": True
}
)
print(response.output_text)
OpenAI 相容-Chat Completions API
通過 enable_search 參數啟用連網搜尋,並將 search_strategy 設定為 agent_max 以啟用網頁抓取功能。同時需要啟用 enable_thinking 參數開啟思考模式。
不支援非流式輸出。
# 匯入依賴與建立用戶端...
completion = client.chat.completions.create(
model="qwen3.7-max",
messages=[{"role": "user", "content": "請訪問阿里雲百鍊代碼解譯器部分的官方文檔,並總結主要內容"}],
extra_body={
"enable_thinking": True,
"enable_search": True,
"search_options": {"search_strategy": "agent_max"}
},
stream=True
)
DashScope
通過 enable_search 參數啟用連網搜尋,並將 search_strategy 設定為 agent_max 以啟用網頁抓取功能。同時需要啟用 enable_thinking 參數開啟思考模式。
不支援非流式輸出。
from dashscope import Generation
response = Generation.call(
model="qwen3.7-max",
messages=[{"role": "user", "content": "請訪問阿里雲百鍊代碼解譯器部分的官方文檔,並總結主要內容"}],
enable_search=True,
search_options={"search_strategy": "agent_max"},
enable_thinking=True,
result_format="message",
stream=True,
incremental_output=True
)
支援的模型
推薦模型
Responses API
千問Max:Qwen3.7-Max系列
千問Plus:Qwen3.7-Plus系列、Qwen3.6-Plus系列、Qwen3.5-Plus系列
Chat Completions API / DashScope
-
千問Max(思考模式):Qwen3-Max系列
-
千問Plus:Qwen3.6-Plus系列、Qwen3.5-Plus系列
其他模型
以下模型也支援此工具調用,但效果不如推薦模型。
-
千問Flash:Qwen3.6-Flash系列、Qwen3.5-Flash系列
-
Qwen3.6開源系列(qwen3.6-27b除外)
-
Qwen3.5開源系列
快速開始
運行以下代碼,通過 Responses API 呼叫網頁抓取工具,自動總結一篇技術文檔。
需要已擷取API Key並配置API Key到環境變數。
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="qwen3.7-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 = response.usage
print("="*20+"工具調用次數"+"="*20)
if hasattr(usage, 'x_tools') and usage.x_tools:
print(f"\n網頁抓取運行次數: {usage.x_tools.get('web_extractor', {}).get('count', 0)}")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: "qwen3.7-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.usage && response.usage.x_tools) {
console.log(`網頁抓取次數: ${response.usage.x_tools.web_extractor?.count || 0}`);
console.log(`連網搜尋次數: ${response.usage.x_tools.web_search?.count || 0}`);
}
// 取消以下注釋查看中間過程的輸出
// console.log(JSON.stringify(response.output[0], null, 2));
}
main();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": "qwen3.7-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="qwen3.7-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 = chunk.response.usage
if hasattr(usage, 'x_tools') and usage.x_tools:
print(f"網頁抓取次數: {usage.x_tools.get('web_extractor', {}).get('count', 0)}")
print(f"連網搜尋次數: {usage.x_tools.get('web_search', {}).get('count', 0)}")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: "qwen3.7-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.usage;
if (usage && usage.x_tools) {
console.log(`網頁抓取次數: ${usage.x_tools.web_extractor?.count || 0}`);
console.log(`連網搜尋次數: ${usage.x_tools.web_search?.count || 0}`);
}
}
}
}
main();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": "qwen3.7-max",
"input": "請訪問阿里雲百鍊代碼解譯器部分的官方文檔,並總結主要內容",
"tools": [
{"type": "web_search"},
{"type": "web_extractor"},
{"type": "code_interpreter"}
],
"enable_thinking": true,
"stream": true
}'OpenAI 相容-Chat Completions 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.chat.completions.create(
model="qwen3.7-max",
messages=[
{"role": "user", "content": "請訪問阿里雲百鍊代碼解譯器部分的官方文檔,並總結主要內容"}
],
extra_body={
"enable_thinking": True,
"enable_search": True,
"search_options": {"search_strategy": "agent_max"}
},
stream=True
)
reasoning_started = False
output_started = False
for chunk in stream:
if chunk.choices:
delta = chunk.choices[0].delta
# 列印思考過程
if hasattr(delta, 'reasoning_content') and delta.reasoning_content:
if not reasoning_started:
print("="*20 + "思考過程" + "="*20)
reasoning_started = True
print(delta.reasoning_content, end='', flush=True)
# 列印回複內容
if delta.content:
if not output_started:
print("\n" + "="*20 + "回複內容" + "="*20)
output_started = True
print(delta.content, end='', flush=True)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.chat.completions.create({
model: "qwen3.7-max",
messages: [
{ role: "user", content: "請訪問阿里雲百鍊代碼解譯器部分的官方文檔,並總結主要內容" }
],
enable_thinking: true,
enable_search: true,
search_options: { search_strategy: "agent_max" },
stream: true
});
let reasoningStarted = false;
let outputStarted = false;
for await (const chunk of stream) {
if (chunk.choices && chunk.choices.length > 0) {
const delta = chunk.choices[0].delta;
// 列印思考過程
if (delta.reasoning_content) {
if (!reasoningStarted) {
console.log("====================思考過程====================");
reasoningStarted = true;
}
process.stdout.write(delta.reasoning_content);
}
// 列印回複內容
if (delta.content) {
if (!outputStarted) {
console.log("\n" + "====================回複內容====================");
outputStarted = true;
}
process.stdout.write(delta.content);
}
}
}
}
main();curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.7-max",
"messages": [
{"role": "user", "content": "請訪問阿里雲百鍊代碼解譯器部分的官方文檔,並總結主要內容"}
],
"enable_thinking": true,
"enable_search": true,
"search_options": {"search_strategy": "agent_max"},
"stream": true
}'DashScope
不支援 Java SDK。
import os
import dashscope
from dashscope import Generation
# 若沒有配置環境變數,請用百鍊API Key將下行替換為:dashscope.api_key = "sk-xxx"
dashscope.api_key = os.getenv("DASHSCOPE_API_KEY")
# 以下為新加坡地區的配置,調用時請將WorkspaceId替換為真實的業務空間ID,各地區的配置不同。
dashscope.base_http_api_url = "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1"
response = Generation.call(
model="qwen3.7-max",
messages=[
{"role": "user", "content": "請訪問阿里雲百鍊代碼解譯器部分的官方文檔,並總結主要內容"}
],
enable_search=True,
search_options={"search_strategy": "agent_max"},
enable_thinking=True,
result_format="message",
stream=True,
incremental_output=True
)
reasoning_started = False
output_started = False
for chunk in response:
if chunk.status_code == 200:
message = chunk.output.choices[0].message
# 列印思考過程
if hasattr(message, 'reasoning_content') and message.reasoning_content:
if not reasoning_started:
print("="*20 + "思考過程" + "="*20)
reasoning_started = True
print(message.reasoning_content, end='', flush=True)
# 列印回複內容
if hasattr(message, 'content') and message.content:
if not output_started:
print("\n" + "="*20 + "回複內容" + "="*20)
output_started = True
print(message.content, end='', flush=True)
else:
print(f"\n請求失敗: code={chunk.code}, message={chunk.message}")
breakcurl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "X-DashScope-SSE: enable" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.7-max",
"input": {
"messages": [
{
"role": "user",
"content": "請訪問阿里雲百鍊代碼解譯器部分的官方文檔,並總結主要內容"
}
]
},
"parameters": {
"enable_thinking": true,
"enable_search": true,
"search_options": {
"search_strategy": "agent_max"
},
"result_format": "message"
}
}'計費說明
計費涉及以下方面:
-
模型調用費用:抓取的網頁內容會拼接到提示詞中,增加模型的輸入 Token,按照模型的標準價格計費。價格詳情請參考百鍊控制台。
-
工具調用費用:包含網頁抓取與連網搜尋的費用。
-
連網搜尋工具每 1000 次調用費用:
-
中國內地和全球部署範圍:$0.57341。
-
國際部署範圍:$10.00。
-
-
網頁抓取工具限時免費。
-