首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 獲取百度網盤真實下載鏈接:HTML 解析、API 模擬與簽名參數提取完整實作

     編輯:Java 相關知識

前言與限制

百度網盤分享頁面為動態渲染,直連下載需模擬瀏覽器請求流程。本實作基於 Jsoup 解析分享頁 + 模擬內部 API 請求。已知限制:同 IP 短時間請求 3 次後觸發驗證碼,需引入驗證碼識別或代理池方可量產。

核心流程

  1. 請求分享頁 HTML,提取 yunData.SIGN、TIMESTAMP、SHARE_ID、uk、shareid、fs_id 等關鍵參數
  2. 呼叫 /share/list 獲取檔案列表(含 fs_id)
  3. 對每個 fs_id 呼叫 /api/sharedownload 獲取真實 dlink

關鍵程式碼

1. HTTP 工具類

public class HttpRequest {
    public static String get(String url) throws Exception {
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5000);
        conn.setReadTimeout(10000);
        try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"))) {
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) sb.append(line).append('\n');
            return sb.toString();
        }
    }
}

2. 核心解析邏輯

public class BaiduNetDisk {
    public static List> getRealUrls(String shareUrl) throws Exception {
        List> result = new ArrayList<>();
        List fsIds = new ArrayList<>();

        // 1. 解析分享頁面提取參數
        Document doc = Jsoup.connect(shareUrl).timeout(10000).get();
        String html = doc.html();

        // 使用正則提取關鍵 JS 變數
        String sign = extract(html, "yunData\\.SIGN\\s*=\\s*\"([^\"]+)\"");
        String timestamp = extract(html, "yunData\\.TIMESTAMP\\s*=\\s*\"([^\"]+)\"");
        String shareId = extract(html, "yunData\\.SHARE_ID\\s*=\\s*\"([^\"]+)\"");
        String uk = extract(html, "\"uk\":\\s*(\\d+)");
        String shareid = extract(html, "\"shareid\":\\s*(\\d+)");
        String appId = extract(html, "\"app_id\":\\s*(\\d+)");

        // 2. 獲取檔案列表(處理目錄遞歸)
        String listApi = "https://pan.baidu.com/share/list?uk=" + uk + "&shareid=" + shareid 
                       + "&page=1&num=100&order=time&desc=1&_=" + timestamp
                       + "&bdstoken=xxx&channel=chunlei&clienttype=0&web=1&app_id=" + appId;
        String listJson = HttpRequest.get(listApi);
        JSONArray list = new JSONObject(listJson).getJSONArray("list");

        for (int i = 0; i < list.length(); i++) {
            JSONObject f = list.getJSONObject(i);
            if ("1".equals(f.optString("isdir"))) {
                // 目錄:遞歸或展開(此處略,實際需分頁獲取)
                continue;
            }
            String fsId = f.getString("fs_id");
            String fileName = f.getString("server_filename");
            fsIds.add(fsId);

            // 3. 獲取真實下載鏈接
            String downApi = "https://pan.baidu.com/api/sharedownload?sign=" + sign
                           + "×tamp=" + timestamp + "&bdstoken=xxx&channel=chunlei"
                           + "&clienttype=0&web=1&app_id=250528&encrypt=0&product=share"
                           + "&uk=" + uk + "&primaryid=" + shareId + "&fid_list=%5B" + fsId + "%5D";
            String downJson = HttpRequest.get(downApi);
            JSONObject down = new JSONObject(downJson);
            if ("0".equals(down.optString("errno"))) {
                String dlink = down.getJSONArray("list").getJSONObject(0).getString("dlink");
                Map map = new HashMap<>();
                map.put("fileName", fileName);
                map.put("url", dlink);
                result.add(map);
            } else if ("-20".equals(down.optString("errno"))) {
                throw new RuntimeException("觸發驗證碼限制,需人工介入或代理池");
            }
        }
        return result;
    }

    private static String extract(String html, String regex) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(html);
        return m.find() ? m.group(1) : "";
    }
}

關鍵參數說明

參數來源用途
sign / timestamp分頁 JS 變數下載 API 簽名驗證
uk / shareid分頁 JSON / JS用戶唯一標識 / 分享唯一標識
fs_id/share/list 回傳檔案唯一標識,下載 API 需要
bdstokenCookie / 頁面防 CSRF token,實際需從 Cookie 獲取
app_id頁面 JSON客戶端標識(250528 為網頁版)

反爬蟲對策(生產級必備)

  • IP 代理池:輪換出口 IP,避免單 IP 觸發驗證碼
  • 請求節流:隨機延遲 2-5 秒,模擬人類行為
  • 完整 Header 模擬:User-Agent、Referer、Cookie(含 BDUSS)、Accept-Language
  • 驗證碼識別:接入 OCR 服務(如百度 AI、騰訊雲)自動破解
  • 瀏覽器自動化:Selenium/Playwright 真實瀏覽器環境,繞過 JS 混淆與指紋檢測

法律與道德提醒

百度網盤用戶協議禁止未授權爬蟲與下載工具。本代碼僅供技術研究學習,嚴禁用於商業用途、大規模分發或侵犯版權內容下載。請尊重知識產權,支持正版。

結語

獲取百度網盤真鏈核心在於參數提取準確性與反爬蟲對抗。JS 逆向分析(sign 生成算法、bdstoken 獲取)是進階關鍵。建議優先使用官方開放 API(如百度雲開放平台)或用戶授權 OAuth 流程,合規且穩定。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved