全部產品
Search
文件中心

Alibaba Cloud Model Studio:網頁抓取

更新時間:Feb 25, 2026

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

使用方式

網頁抓取功能支援三種調用方式,啟用參數有所不同:

OpenAI 相容-Responses API

要啟用網頁抓取功能,您需要在 tools 參數中同時添加 web_search(連網搜尋)和 web_extractor(網頁抓取)工具。

當使用 qwen3-max-2026-01-23 時,需要啟用 enable_thinking 參數以開啟思考模式。
為獲得最佳回複效果,尤其是在解決數學計算、資料分析類問題時,建議同時開啟 code_interpreter 工具。這將允許模型在需要時調用代碼解譯器,提高結果的準確性。
# 匯入依賴與建立用戶端...
response = client.responses.create(
    model="qwen3-max-2026-01-23",
    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-max-2026-01-23",
    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-max-2026-01-23",
    messages=[{"role": "user", "content": "請訪問阿里雲百鍊代碼解譯器部分的官方文檔,並總結主要內容"}],
    enable_search=True,
    search_options={"search_strategy": "agent_max"},
    enable_thinking=True,
    result_format="message",
    stream=True,
    incremental_output=True
)

支援的模型

  • 千問Max:思考模式下的qwen3-maxqwen3-max-2026-01-23

  • 千問Plus:qwen3.5-plusqwen3.5-plus-2026-02-15

  • 千問Flash:qwen3.5-flashqwen3.5-flash-2026-02-23

  • 千問開源模型:qwen3.5-397b-a17bqwen3.5-122b-a10bqwen3.5-27bqwen3.5-35b-a3b

快速開始

運行以下代碼,通過 Responses API 呼叫網頁抓取工具,自動總結一篇技術文檔。

需要已擷取API Key配置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"),
    base_url="https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
)

response = client.responses.create(
    model="qwen3-max-2026-01-23",
    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,
    baseURL: "https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});

async function main() {
    const response = await openai.responses.create({
        model: "qwen3-max-2026-01-23",
        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://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-max-2026-01-23",
    "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"),
    base_url="https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
)
    
stream = client.responses.create(
    model="qwen3-max-2026-01-23",
    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,
    baseURL: "https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});

async function main() {
    const stream = await openai.responses.create({
        model: "qwen3-max-2026-01-23",
        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://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-max-2026-01-23",
    "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"),
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)

stream = client.chat.completions.create(
    model="qwen3-max-2026-01-23",
    messages=[
        {"role": "user", "content": "請訪問阿里雲百鍊代碼解譯器部分的官方文檔,並總結主要內容"}
    ],
    extra_body={
        "enable_search": True,
        "search_options": {"search_strategy": "agent_max"}
    },
    stream=True
)

for chunk in stream:
    print(chunk)
import OpenAI from "openai";
import process from 'process';

const openai = new OpenAI({
    // 若沒有配置環境變數,請用百鍊API Key將下行替換為:apiKey: "sk-xxx",
    apiKey: process.env.DASHSCOPE_API_KEY,
    baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1"
});

async function main() {
    const stream = await openai.chat.completions.create({
        model: "qwen3-max-2026-01-23",
        messages: [
            { role: "user", content: "請訪問阿里雲百鍊代碼解譯器部分的官方文檔,並總結主要內容" }
        ],
        enable_search: true,
        search_options: { search_strategy: "agent_max" },
        stream: true
    });

    for await (const chunk of stream) {
        console.log(chunk);
    }
}

main();
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-max-2026-01-23",
    "messages": [
        {"role": "user", "content": "請訪問阿里雲百鍊代碼解譯器部分的官方文檔,並總結主要內容"}
    ],
    "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")

response = Generation.call(
    model="qwen3-max-2026-01-23",
    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
last_usage = None

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)

        # 儲存最後的 usage 資訊
        if hasattr(chunk, 'usage') and chunk.usage:
            last_usage = chunk.usage

# 列印工具調用次數
if last_usage:
    print("\n" + "="*20 + "工具調用次數" + "="*20)
    if hasattr(last_usage, 'plugins') and last_usage.plugins:
        print(f"網頁抓取次數: {last_usage.plugins.get('web_extractor', {}).get('count', 0)}")
curl -X POST https://dashscope.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-max-2026-01-23",
    "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

    • 網頁抓取工具限時免費。