Skip to main content

網頁抓取

大模型無法直接擷取網頁資料。網頁抓取工具可以訪問指定 URL 並提取內容,為大模型提供所需資訊。

使用方式

網頁抓取功能支援透過 Responses API 或 Anthropic 相容 Messages API 呼叫,兩種方式的工具名稱和啟用參數有所不同:
  • OpenAI 相容-Responses API
  • Anthropic 相容
要啟用網頁抓取功能,您需要在 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)

支援的模型

  • 千問 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 呼叫網頁抓取工具,自動總結一篇技術文檔。
需要已擷取與配置 API Key並配置API Key到環境變數。
python
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')}")
運行以上代碼可擷取如下回複:
====================回複內容====================
根據阿里雲百鍊官方文檔,我為您總結了**代碼解譯器**功能的核心內容:

## 一、功能定位

...

> **文檔來源**:阿里雲百鍊官方文檔 - [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
python
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')}")

計費說明

計費涉及以下方面:
  • 模型調用費用:抓取的網頁內容會拼接到提示詞中,增加模型的輸入 Token,按照模型的標準價格計費。價格詳情請參考百鍊控制台。
  • 工具調用費用:包含網頁抓取與連網搜尋的費用。
    • 連網搜尋工具每 1000 次調用費用:
      • 華北2(北京)地區:$0.57341。
      • 新加坡地區:$10.00。
    • 網頁抓取工具限時免費。