用python解密百度指数数据

获取数据

使用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+G6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VVCBB6CBB6CB+hCB+hCBh6CBZhCBBVCBBhCBBDCBBhCBB+CBBhCB++CB+BCBBGCBldCBB+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

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要获取百度指数数据,可以通过百度指数API或者爬取百度指数网页来实现。以下是通过爬取百度指数网页的方法: 1. 首先需要安装requests和beautifulsoup4库,可以通过以下命令安装: ``` pip install requests pip install beautifulsoup4 ``` 2. 接着,需要登录百度指数网页(http://index.baidu.com/),并在浏览器中打开开发者工具(F12),选择“网络”选项卡,然后在搜索框中输入关键词,点击搜索按钮。在“网络”选项卡中可以看到一些请求,找到“index”开头的请求,右键选择“Copy” -> “Copy as cURL”。 3. 在Python中,可以使用requests库模仿浏览器发送请求,将刚才复制的cURL命令转换为requests请求: ```python import requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} cookies = { 'Cookie': 'BAIDUID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:FG=1; BIDUPSID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; PSTM=xxxxxxx; BDUSS=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'} url = 'http://index.baidu.com/api/SearchApi/index?' params = { 'word': '关键词', 'area': 0, 'startDate': '20210101', 'endDate': '20211231' } response = requests.get(url, headers=headers, cookies=cookies, params=params) print(response.text) ``` 其中,需要将cURL命令中的Cookie和请求参数拷贝到Python代码中。 4. 解析响应数据,可以使用beautifulsoup4库进行解析,例如: ```python from bs4 import BeautifulSoup soup = BeautifulSoup(response.text, 'html.parser') print(soup.prettify()) ``` 其中,可以根据网页结构使用合适的方法解析出需要的数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值