大模型無法直接擷取網頁資料。網頁抓取工具可以訪問指定 URL 並提取內容,為大模型提供所需資訊。
使用方式
網頁抓取功能支援三種調用方式,啟用參數有所不同:- OpenAI 相容-Responses API
- OpenAI 相容-Chat Completions API
- DashScope
要啟用網頁抓取功能,您需要在
tools 參數中同時添加 web_search(連網搜尋)和 web_extractor(網頁抓取)工具。
當使用 qwen3-max-2026-01-23 時,需要啟用 enable_thinking 參數以開啟思考模式。
為獲得最佳回複效果,尤其是在解決數學計算、資料分析類問題時,建議同時開啟 code_interpreter 工具。這將允許模型在需要時調用代碼解譯器,提高結果的準確性。
Copy
# 匯入依賴與建立用戶端...
response = client.responses.create(
model="qwen3.8-max",
input="請訪問阿里雲百鍊代碼解譯器部分的官方文檔,並總結主要內容",
tools=[
# 開啟網頁抓取必須同時開啟連網搜尋工具
{"type": "web_search"},
{"type": "web_extractor"},
{"type": "code_interpreter"}
],
extra_body={
# 必須開啟思考模式
"enable_thinking": True
}
)
print(response.output_text)
通過
enable_search 參數啟用連網搜尋,並將 search_strategy 設定為 agent_max 以啟用網頁抓取功能。同時需要啟用 enable_thinking 參數開啟思考模式。不支援非流式輸出。
Copy
# 匯入依賴與建立用戶端...
completion = client.chat.completions.create(
model="qwen3-max",
messages=[{"role": "user", "content": "請訪問阿里雲百鍊代碼解譯器部分的官方文檔,並總結主要內容"}],
extra_body={
"enable_thinking": True,
"enable_search": True,
"search_options": {"search_strategy": "agent_max"}
},
stream=True
)
通過
enable_search 參數啟用連網搜尋,並將 search_strategy 設定為 agent_max 以啟用網頁抓取功能。同時需要啟用 enable_thinking 參數開啟思考模式。不支援非流式輸出。
Copy
from dashscope import Generation
response = Generation.call(
model="qwen3-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
- Chat Completions API / DashScope
千問Max:Qwen3.8-Max系列、Qwen3.7-Max系列千問Plus:Qwen3.7-Plus系列、Qwen3.6-Plus系列、Qwen3.5-Plus系列DeepSeek:deepseek-v4-flash、deepseek-v4-flash-0731Qwen3.8開源系列
- 千問Max(思考模式):Qwen3-Max系列
- 千問Plus:Qwen3.5-Plus系列
其他模型
以下模型也支援此工具調用,但效果不如推薦模型。僅支援通過Responses API調用。- 千問Flash:Qwen3.7-Flash系列、Qwen3.6-Flash系列、Qwen3.5-Flash系列
- Qwen3.6開源系列(qwen3.6-27b除外)
- Qwen3.5開源系列
快速開始
運行以下代碼,通過 Responses API 呼叫網頁抓取工具,自動總結一篇技術文檔。需要已擷取與配置 API Key並配置API Key到環境變數。
Copy
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.8-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)}")
Copy
====================回複內容====================
根據阿里雲百鍊官方文檔,我為您總結了**代碼解譯器**功能的核心內容:
## 一、功能定位
...
> **文檔來源**:阿里雲百鍊官方文檔 - [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
- OpenAI 相容-Chat Completions API
- DashScope
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.8-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)}")
Copy
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-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)
不支援 Java SDK。
Copy
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-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}")
break
計費說明
計費涉及以下方面:- 模型調用費用:抓取的網頁內容會拼接到提示詞中,增加模型的輸入 Token,按照模型的標準價格計費。價格詳情請參考百鍊控制台。
-
工具調用費用:包含網頁抓取與連網搜尋的費用。
-
連網搜尋工具每 1000 次調用費用:
- 華北2(北京)地區:$0.57341。
- 新加坡地區:$10.00。
- 網頁抓取工具限時免費。
-
連網搜尋工具每 1000 次調用費用: