百度指数解密


我直接用大佬的代码,自己懒得写了。。。

import requests
import sys
import time
from datetime import date

word_url = 'http://index.baidu.com/api/SearchApi/thumbnail?area=0&word={}'
COOKIES = 'BAIDUID=9DC4D47B123B050A090109FA2AE2A754:FG=1; BDUSS=jM4Sn5-eTZXa0dMMU5sSWJORnVPbUkzSEIySXZHMGpHa1dOVURlT0NFN3l6SnBlSVFBQUFBJCQAAAAAAAAAAAEAAACRVd1hsKXEsMSwZmZmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPI~c17yP3NeR; BIDUPSID=9DC4D47B123B050A090109FA2AE2A754; PSTM=1584611994; H_PS_PSSID=30969_1446_21087_30902_30824_31085; bdindexid=1bnsqsgnfsm0lps91jud04m9b6; bdshare_firstime=1584614742870; Hm_lvt_d101ea4d2a5c67dab98251f0b5de24dc=1584612146,1584614564,1584614743,1584615274; indexpro=k9c0uudcv6ahp351cih0elnpi5; Hm_lpvt_d101ea4d2a5c67dab98251f0b5de24dc=1584619411; RT="z=1&dm=baidu.com&si=3s6f8sfz619&ss=k7yl5oyr&sl=0&tt=0&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=1ir&ul=59dfi"'


def decrypt(t, e):
    n = list(t)
    i = list(e)
    a = {}
    result = []
    ln = int(len(n) / 2)
    start = n[ln:]
    end = n[:ln]
    for j, k in zip(start, end):
        a.update({k: j})
    for j in e:
        result.append(a.get(j))
    return ''.join(result)


def get_index_home(keyword):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36',
        'Cookie': COOKIES
    }
    resp = requests.get(word_url.format(keyword), headers=headers)
    j = resp.json()
    uniqid = j.get('data').get('uniqid')
    return get_ptbk(uniqid)


def get_ptbk(uniqid):
    url = 'http://index.baidu.com/Interface/ptbk?uniqid={}'
    ptbk_headers = {
        'Accept': 'application/json, text/plain, */*',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'no-cache',
        'Cookie': COOKIES,
        'DNT': '1',
        'Host': 'index.baidu.com',
        'Pragma': 'no-cache',
        'Proxy-Connection': 'keep-alive',
        'Referer': 'http://index.baidu.com/v2/index.html',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest',
    }
    resp = requests.get(url.format(uniqid), headers=ptbk_headers)
    if resp.status_code != 200:
        print('获取uniqid失败')
        sys.exit(1)
    return resp.json().get('data')


def get_index_data(keyword, start='2011-01-01', end='2020-03-19'):
    url = f'http://index.baidu.com/api/SearchApi/index?word={keyword}&area=0&startDate={start}&endDate={end}'
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'no-cache',
        'Cookie': COOKIES,
        'DNT': '1',
        'Host': 'index.baidu.com',
        'Pragma': 'no-cache',
        'Proxy-Connection': 'keep-alive',
        'Referer': 'http://index.baidu.com/v2/index.html',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest',
    }
    resp = requests.get(url, headers=headers)
    if resp.status_code != 200:
        print('获取指数失败')
        sys.exit(1)
    data = resp.json().get('data').get('userIndexes')[0]
    uniqid = data.get('uniqid')
    ptbk = get_index_home(uniqid)
    while ptbk is None or ptbk == '':
        ptbk = get_index_home(uniqid)
    all_data = data.get('all').get('data')
    result = decrypt(ptbk, all_data)
    result = result.split(',')
    print(len(result))
    print(result)


if __name__ == '__main__':
    get_index_data('bilibili')
    

文章作者: 小游
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小游 !
  目录