萬盛學電腦網

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

PHP 獲取愛站與站長之家 (Chinaz) 百度權重:API 呼叫與正則解析實作

百度權重說明

「百度權重」非百度官方指標,而是愛站網、站長之家等第三方工具根據關鍵字排名預估流量換算的權重值 (0~9)。兩站算法不同,數值常有出入,僅供參考。

愛站網 (aizhan.com) 介面

<?php
/**
 * 獲取愛站百度權重
 * @param string $domain 完整域名,如 www.example.com
 * @return int 權重值 0-9,失敗回傳 0
 */
function getAizhanBaiduWeight($domain) {
    $url = "https://www.aizhan.com/getbr.php?url=" . urlencode($domain) . "&style=1";
    $data = @file_get_contents($url);
    if ($data === false) return 0;

    // 回傳格式通常為: 或純數字,視 style 參數而定
    // style=1 回傳純數字文字
    if (preg_match('/^\d+$/', trim($data))) {
        return (int)trim($data);
    }
    // 兼容舊版 HTML 格式
    if (preg_match('/>(\d+)<\/a>/i', $data, $m)) {
        return (int)$m[1];
    }
    return 0;
}

站長之家 (chinaz.com) 介面

<?php
/**
 * 獲取 Chinaz 百度權重
 * @param string $domain 域名
 * @return int 權重值 0-9
 */
function getChinazBaiduWeight($domain) {
    $url = "https://mytool.chinaz.com/baidusort.aspx?host=" . urlencode($domain);
    $html = @file_get_contents($url);
    if ($html === false) return 0;

    // 頁面結構會變動,需定期維護正則
    // 典型特徵:4 或 
4
$patterns = [ '/]*class=["\']br["\'][^>]*>(\d+)<\/span>/i', '/]*id=["\']baidu_pr["\'][^>]*>(\d+)<\/div>/i', '/百度權重[^>]*>(\d+)

統一介面與快取策略

<?php
class BaiduWeightFetcher {
    private $cache_dir;
    private $ttl = 86400; // 24 小時快取

    public function __construct($cache_dir = '/tmp/baidu_weight_cache/') {
        $this->cache_dir = rtrim($cache_dir, '/') . '/';
        if (!is_dir($this->cache_dir)) mkdir($this->cache_dir, 0755, true);
    }

    public function getWeight($domain, $source = 'aizhan') {
        $cache_file = $this->cache_dir . md5($source . ':' . $domain) . '.json';
        // 讀取快取
        if (is_file($cache_file) && (time() - filemtime($cache_file) < $this->ttl)) {
            $cached = json_decode(file_get_contents($cache_file), true);
            if ($cached !== null) return $cached['weight'];
        }

        // 即時抓取
        $weight = 0;
        if ($source === 'aizhan') {
            $weight = $this->fetchAizhan($domain);
        } elseif ($source === 'chinaz') {
            $weight = $this->fetchChinaz($domain);
        }

        // 寫入快取
        @file_put_contents($cache_file, json_encode(['weight' => $weight, 'time' => time()]));
        return $weight;
    }

    private function fetchAizhan($domain) { /* ... 同上 getAizhanBaiduWeight ... */ }
    private function fetchChinaz($domain) { /* ... 同上 getChinazBaiduWeight ... */ }
}

// 使用
$fetcher = new BaiduWeightFetcher();
echo "愛站權重: " . $fetcher->getWeight('www.example.com', 'aizhan');
echo "Chinaz 權重: " . $fetcher->getWeight('www.example.com', 'chinaz');

注意事項與風險

  • 反爬蟲機制:高頻請求會觸發驗證碼/IP 封禁,必須快取、限流 (建議 > 1 小時/次/域名)。
  • 頁面結構變更:正則易失效,建議配合 DOM 解析器 (DOMDocument/symfony/dom-crawler) 提高魯棒性。
  • 法律合規:遵守目標站 robots.txt 及服務條款,商業用途請購買官方 API。
  • 數據參考性:權重波動大,決策時請結合流量、收錄、關鍵字排名綜合判斷。

官方 API 替代方案

  • 愛站開放平台:open.aizhan.com (需企業認證)
  • 站長之家 API:api.chinaz.com (付費)
  • DataForSEO / Semrush / Ahrefs:提供更標準化、穩定的權威指標 (DR/DA/AS 等)。

建議生產系統優先採用官方付費 API,免費爬蟲僅作內部輔助參考。

copyright © 萬盛學電腦網 all rights reserved