获取数据
使用chrome浏览器的开发者工具,获取请求数据。注意一共有两个请求,https://index.baidu.com/api/SearchApi/index 和 https://index.baidu.com/Interface/ptbk。
编写代码解密
import json
#解密算法
def decrypt(ptbk, index_data):
n = len(ptbk)//2
a = dict(zip(ptbk[:n], ptbk[n:]))
d = "".join([a[s] for s in index_data])
return d
def convertdata(e):
if len(e) == 0:
#空值处理
return 0
else:
return int(e)
def get_index_data(indexData,ptbkData):
data = json.loads(indexData)
# print(data)
uniqid = data['data']['uniqid']
print(uniqid)
data2 = json.loads(ptbkData)
ptbk = data2['data']
print(ptbk)
result = {}
for userIndexe in data['data']['userIndexes']:
name = userIndexe['word'][0]['name']
tmp = {}
index_all = userIndexe['all']['data']
index_all_data = [ convertdata(e) for e in decrypt(ptbk, index_all).split(",")]
tmp["all"] = index_all_data
index_pc = userIndexe['pc']['data']
index_pc_data = [convertdata(e) for e in decrypt(ptbk, index_pc).split(",")]
tmp["pc"] = index_pc_data
index_wise = userIndexe['wise']['data']
index_wise_data = [convertdata(e)
for e in decrypt(ptbk, index_wise).split(",")]
tmp["wise"] = index_wise_data
result[name] = tmp
return result
# pc:表示pc端,wise表示移动端,all表示pc端+移动端
# 从浏览器的请求数据复制过来就可以解密
data1 = r'{"status":0,"data":{"userIndexes":[{"word":[{"name":"\u65b0\u51a0","wordType":1}],"all":{"startDate":"2022-01-07","endDate":"2022-11-16","data":"+dGC+dGC+D6ChB+C+VGC+DVC+GBChlBC+6lC+6hC+V6ChlBC+dBC+dDC+BVC++VC+llC+ZDC+Z+C+ZhC++BC++GCBVhCBDDCBBVCBD+C+llCBGBCBDZCBDVC+lZC+6lC+lVC+B6C+BVC+hVC+BhCBVBC+hlC++hC++ZC+lZC+dlC+BZC+BGC+hGC+ZGC+h+C+B+C+ldC+h+C++ZC+ZdC+BVC++ZC+DZC+dBC+BGC++GC+d6C+GBC+G6ChZhCZVGCZ6DCD+DCVGGCDdhCDVlCBBZhCBlh+CBllhCVdZCDZ6CdVdCdBVCdDBC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BB+CBl6CBldCBl+CVBCZldCZG6CZ6GCZGDChDZCZ++ChVhChDGChGlChhDC+VBC+dBCBVVC++lC++BCBVlC+hVC+ldC+B6C+B+CBGDCBDdC+BBC+ZhC++l"},"type":"day"}],"generalRatio":[{"word":[{"name":"\u65b0\u51a0","wordType":1}],"all":{"avg":264,"yoy":-1,"qoq":1},"pc":{"avg":83,"yoy":-6,"qoq":-3},"wise":{"avg":180,"yoy":"-","qoq":3}}],"uniqid":"5c0b747797bf50e0028071f210549e13"},"logid":2197115005,"message":0}'
data2 = r'{"status":0,"data":"mVC+ZwB6Dlh7Gd,-9,24%15803+76."}'
print(get_index_data(data1,data2))
解密结果
参考文章
https://blog.51cto.com/u_11866025/5714481