萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PHP 正則抓取淘寶商品價格:從入門到反爬蟲對抗實戰

前言:原始代碼的局限與風險

原文提供的 taoBaoJiaGe() 使用 file_get_contents + 正則提取價格,存在嚴重問題:

  • ❌ **無超時控制**:對方不回應腳本卡死
  • ❌ **無 User-Agent**:極易被防火牆/反爬蟲攔截 (403/503)
  • ❌ **正則脆弱**:頁面結構微調即失效 (如 class 名變更、多層包裹)
  • ❌ **無 Cookie/登入態**:無法獲取會員價、優惠價、SKU 價格區間
  • ❌ **法律/合規風險**:淘寶/天貓明確禁止未授權爬蟲,需遵守 robots.txt、User-Agent 聲明、頻率限制

現代化穩健方案:cURL + 選擇器 (DOMDocument/XPath) + 反爬基礎對抗

<?php
declare(strict_types=1);

class TaobaoPriceFetcher
{
    private int $timeout = 10;
    private string $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
    private string $cookieFile; // 支援 Cookie 持久化

    public function __construct(string $cookieFile = '')
    {
        $this->cookieFile = $cookieFile ?: sys_get_temp_dir() . '/tb_cookies.txt';
    }

    /**
     * 獲取商品價格資訊
     * @return array ['price' => '199.00', 'original_price' => '299.00', 'currency' => 'CNY', 'sku_prices' => [...]] 或拋出異常
     */
    public function fetch(string $itemUrl): array
    {
        $html = $this->request($itemUrl);
        return $this->parsePrice($html, $itemUrl);
    }

    private function request(string $url): string
    {
        $ch = curl_init($url);
        curl_setopt_array($ch, [
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_MAXREDIRS      => 5,
            CURLOPT_CONNECTTIMEOUT => $this->timeout,
            CURLOPT_TIMEOUT        => $this->timeout,
            CURLOPT_SSL_VERIFYPEER => true,
            CURLOPT_USERAGENT      => $this->userAgent,
            CURLOPT_COOKIEJAR      => $this->cookieFile,
            CURLOPT_COOKIEFILE     => $this->cookieFile,
            CURLOPT_ENCODING       => 'gzip, deflate, br', // 支援壓縮
            CURLOPT_HTTPHEADER     => [
                'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8',
                'Cache-Control: no-cache',
                'Pragma: no-cache',
            ],
        ]);

        $html = curl_exec($ch);
        $errno = curl_errno($ch);
        $error = curl_error($ch);
        $code  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($errno) throw new RuntimeException("cURL Error [$errno]: $error");
        if ($code !== 200) throw new RuntimeException("HTTP $code: " . ($code === 403 ? '疑似反爬攔截' : '請求失敗'));
        if (empty($html)) throw new RuntimeException('Empty response');

        // 簡易反爬檢測:檢查是否為驗證頁/登入頁/錯誤頁
        if (strpos($html, 'nc_') !== false || strpos($html, 'alibaba.com') !== false && strpos($html, 'slide') !== false) {
            throw new RuntimeException('觸發滑塊驗證/反爬頁面,需人工介入或使用無頭瀏覽器');
        }

        return $html;
    }

    private function parsePrice(string $html, string $url): array
    {
        // 策略 1: 現代淘寶/天貓頁面價格通常在 JSON-LD 結構化數據或 window.__INIT_DATA__ 中
        // 這比正則匹配 HTML 標籤穩健得多
        if (preg_match('/window\.__INIT_DATA__\s*=\s*(\{.+?\});/s', $html, $m)) {
            $json = json_decode($m[1], true);
            if ($json) {
                $price = $this->extractPriceFromInitData($json);
                if ($price) return $price;
            }
        }

        // 策略 2: JSON-LD 結構化數據 (SEO 標準)
        if (preg_match_all('/
						
copyright © 萬盛學電腦網 all rights reserved