摘要:大家好我是小小明,今天給大家演示如何使用直接采集百度指數(shù)的數(shù)據(jù)。本文不演示如何使用自動化工具采集百度指數(shù),為了采集更簡單將直接讀取并解析接口。
大家好我是小小明,今天給大家演示如何使用python直接采集百度指數(shù)的數(shù)據(jù)。
百度指數(shù)(Baidu Index) 是以百度海量網(wǎng)民行為數(shù)據(jù)為基礎的數(shù)據(jù)分析平臺,它能夠能夠告訴用戶:某個關鍵詞在百度的搜索規(guī)模有多大,一段時間內的漲跌態(tài)勢以及相關的新聞輿論變化,關注這些詞的網(wǎng)民是什么樣的,分布在哪里,同時還搜了哪些相關的詞。
百分十先生分享過如何使用uiautomation采集百度指數(shù):百度指數(shù) 如何批量獲???
不過個人感覺這方法好像有點殺雞用牛刀,對于網(wǎng)頁使用selenium完全足以,當然對于專門針對selenium進行反爬檢測的網(wǎng)頁就需要特殊修改。
本文不演示如何使用UI自動化工具采集百度指數(shù),為了采集更簡單將直接讀取并解析接口。
關于uiautomation,PC端的UI自動化可以查看教程:Windows桌面程序自動化控制之uiautomation模塊全面講解
打開百度指數(shù)發(fā)現(xiàn)查看指數(shù)必須要先登錄,比如我們對比一個python和Java最近一周的指數(shù):
當鼠標移動到每天的坐標上時會顯示當天的數(shù)據(jù),例如:
如果我們采用UI自動化的方式,至少得模擬移動到每天的坐標。
打開開發(fā)者工具,重新查詢發(fā)現(xiàn)獲取數(shù)據(jù)的接口:
實際的指數(shù)數(shù)據(jù)就存儲在這個data字段中,但是以某種加密方式加密了。
然后注意第二個接口的某個參數(shù)與當前接口返回的數(shù)據(jù)某個值一致。
此時我全局搜索decrypt
,找到了加密函數(shù):
此時打上斷點重新搜索,可以看到傳入該函數(shù)的t參數(shù)與ptbk接口返回的值一致:
說明我們只需要將這段js翻譯為python來解密加密數(shù)據(jù)即可。
下面我們總結一下指數(shù)數(shù)據(jù)獲取的思路:
通過index接口獲取uniqid和加密后的指數(shù)數(shù)據(jù)userIndexes
通過ptbk接口傳入uniqid獲取密鑰key
通過解密函數(shù)根據(jù)密鑰key解密userIndexes
下面我們分別用代碼來實現(xiàn),首先獲取指數(shù)數(shù)據(jù):
import requestsimport jsonheaders = { "Connection": "keep-alive", "Accept": "application/json, text/plain, */*", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36", "Sec-Fetch-Site": "same-origin", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Dest": "empty", "Referer": "https://index.baidu.com/v2/main/index.html", "Accept-Language": "zh-CN,zh;q=0.9", "Cookie": cookie,}words = "[[{"name":"python","wordType":1}],[{"name":"java","wordType":1}]]"start = "2021-11-15"end = "2021-11-21"url = f"http://index.baidu.com/api/SearchApi/index?area=0&word={words}&area=0&startDate={start}&endDate={end}"res = requests.get(url, headers=headers)data = res.json()["data"]data
cookie需要在登錄后復制粘貼獲取,就是請求中的這段字符串(直接復制粘貼即可):
結果:
{"userIndexes": [{"word": [{"name": "python", "wordType": 1}], "all": {"startDate": "2021-11-15", "endDate": "2021-11-21", "data": "WQ3Q-nWQ.yGnWQ.y3nW3yQsnWW.Q-nysXV3ny.-VG"}, "pc": {"startDate": "2021-11-15", "endDate": "2021-11-21", "data": "y3yVXny3yWyny3GWWny3QyVnyQG33nXGsQn-..G"}, "wise": {"startDate": "2021-11-15", "endDate": "2021-11-21", "data": "XWVXnXQ-XnX3XWnX-WynX3X3n--XynsQyG"}, "type": "day"}, {"word": [{"name": "java", "wordType": 1}], "all": {"startDate": "2021-11-15", "endDate": "2021-11-21", "data": "-XW.n-ssXnXG3GnXG..nXyyGnVQyWn.QQQ"}, "pc": {"startDate": "2021-11-15", "endDate": "2021-11-21", "data": ".VVVn.3Xsn.XX3n.-VWn.sW3nQG-snWVWQ"}, "wise": {"startDate": "2021-11-15", "endDate": "2021-11-21", "data": "QW.XnQW-WnQG3VnQyXQnQQ-VnQWW.nWsyG"}, "type": "day"}], "generalRatio": [{"word": [{"name": "python", "wordType": 1}], "all": {"avg": 21565, "yoy": -24, "qoq": 7}, "pc": {"avg": 12470, "yoy": -32, "qoq": 3}, "wise": {"avg": 9095, "yoy": -10, "qoq": 12}}, {"word": [{"name": "java", "wordType": 1}], "all": {"avg": 8079, "yoy": -23, "qoq": 11}, "pc": {"avg": 4921, "yoy": -33, "qoq": 6}, "wise": {"avg": 3157, "yoy": "-", "qoq": 18}}], "uniqid": "5f0a123915325e28d9f055409955c9ad"}
這些數(shù)據(jù)中,wise表示移動端,all表示pc端+移動端。userIndexes是指數(shù)詳情數(shù)據(jù),generalRatio是概覽數(shù)據(jù)。
下面我們只關心各個關鍵字的整體表現(xiàn)。
下面我們獲取uniqid
并獲取ptbk:
uniqid = data["uniqid"]res = requests.get( f"http://index.baidu.com/Interface/ptbk?uniqid={uniqid}", headers=headers)ptbk = res.json()["data"]ptbk
"LV.7yF-s30WXGQn.65+1-874%2903,"
下面我將下面這段Js代碼翻譯為python:
decrypt: function(t, e) { if (t) { for (var n = t.split(""), i = e.split(""), a = {}, r = [], o = 0; o < n.length / 2; o++) a[n[o]] = n[n.length / 2 + o]; for (var s = 0; s < e.length; s++) r.push(a[i[s]]); return r.join("") }}
python代碼:
def decrypt(ptbk, index_data): n = len(ptbk)//2 a = dict(zip(ptbk[:n], ptbk[n:])) return "".join([a[s] for s in index_data])
然后我們遍歷每個關鍵字解密出對應的指數(shù)數(shù)據(jù):
for userIndexe in data["userIndexes"]: name = userIndexe["word"][0]["name"] index_data = userIndexe["all"]["data"] r = decrypt(ptbk, index_data) print(name, r)
python 23438,23510,23514,24137,22538,17964,15860java 8925,8779,9040,9055,9110,6312,5333
檢查實際網(wǎng)頁中的數(shù)據(jù)發(fā)現(xiàn)確實一致:
那么我們就可以輕松獲取任意指定關鍵字的指數(shù)數(shù)據(jù)。下面我將其整體封裝一下,完整代碼為:
import requestsimport jsonfrom datetime import date, timedeltaheaders = { "Connection": "keep-alive", "Accept": "application/json, text/plain, */*", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36", "Sec-Fetch-Site": "same-origin", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Dest": "empty", "Referer": "https://index.baidu.com/v2/main/index.html", "Accept-Language": "zh-CN,zh;q=0.9", "Cookie": cookie,}def decrypt(ptbk, index_data): n = len(ptbk)//2 a = dict(zip(ptbk[:n], ptbk[n:])) return "".join([a[s] for s in index_data])def get_index_data(keys, start=None, end=None): words = [[{"name": key, "wordType": 1}] for key in keys] words = str(words).replace(" ", "").replace(""", "/"") today = date.today() if start is None: start = str(today-timedelta(days=8)) if end is None: end = str(today-timedelta(days=2)) url = f"http://index.baidu.com/api/SearchApi/index?area=0&word={words}&area=0&startDate={start}&endDate={end}" print(words, start, end) res = requests.get(url, headers=headers) data = res.json()["data"] uniqid = data["uniqid"] url = f"http://index.baidu.com/Interface/ptbk?uniqid={uniqid}" res = requests.get(url, headers=headers) ptbk = res.json()["data"] result = {} result["startDate"] = start result["endDate"] = end for userIndexe in data["userIndexes"]: name = userIndexe["word"][0]["name"] tmp = {} index_all = userIndexe["all"]["data"] index_all_data = [int(e) for e in decrypt(ptbk, index_all).split(",")] tmp["all"] = index_all_data index_pc = userIndexe["pc"]["data"] index_pc_data = [int(e) for e in decrypt(ptbk, index_pc).split(",")] tmp["pc"] = index_pc_data index_wise = userIndexe["wise"]["data"] index_wise_data = [int(e) for e in decrypt(ptbk, index_wise).split(",")] tmp["wise"] = index_wise_data result[name] = tmp return result
測試一下:
get_index_data(["python", "java"])
{"startDate": "2021-11-15", "endDate": "2021-11-21", "python": {"all": [23438, 23510, 23514, 24137, 22538, 17964, 15860], "pc": [14169, 14121, 14022, 14316, 13044, 9073, 8550], "wise": [9269, 9389, 9492, 9821, 9494, 8891, 7310]}, "java": {"all": [8925, 8779, 9040, 9055, 9110, 6312, 5333], "pc": [5666, 5497, 5994, 5862, 5724, 3087, 2623], "wise": [3259, 3282, 3046, 3193, 3386, 3225, 2710]}}
結果非常不錯。
文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://www.ezyhdfw.cn/yun/124793.html
摘要:學習爬蟲的背景了解。但是搜索引擎蜘蛛的爬行是被輸入了一定的規(guī)則的,它需要遵從一些命令或文件的內容,如標注為的鏈接,或者是協(xié)議。不同領域不同背景的用戶往往具有不同的檢索目的和需求,搜索引擎無法提供針對具體某個用戶的搜索結果。 學習python爬蟲的背景了解。 大數(shù)據(jù)時代數(shù)據(jù)獲取方式 如今,人類社會已經(jīng)進入了大數(shù)據(jù)時代,數(shù)據(jù)已經(jīng)成為必不可少的部分,可見數(shù)據(jù)的獲取非常重要,而數(shù)據(jù)的獲取的方式...
摘要:本例以數(shù)據(jù)庫為代表,展示將獲取到的股票數(shù)據(jù)存入數(shù)據(jù)庫的方法其他類型數(shù)據(jù)庫請參考官網(wǎng)文檔的部分。存入數(shù)據(jù)庫追加數(shù)據(jù)到現(xiàn)有表財經(jīng)數(shù)據(jù)接口包的使用存入財經(jīng)數(shù)據(jù)接口包的使用 安裝TuShare 方式1:pip install tushare 方式2:訪問https://pypi.python.org/pypi/tushare/下載安裝 方式3:將源代碼下載到本地python setup.py ...
摘要:時間永遠都過得那么快,一晃從年注冊,到現(xiàn)在已經(jīng)過去了年那些被我藏在收藏夾吃灰的文章,已經(jīng)太多了,是時候把他們整理一下了。那是因為收藏夾太亂,橡皮擦給設置私密了,不收拾不好看呀。 ...
眾所周知,Python的一個使用場景還是比較多的,在工作當中,也會涉及到多方面的一些事情。那么,今天小編寫這篇文章的一個主要目的,給大家來介紹關于如何用Python完成百度與搞得地圖轉換,下面就給大家詳細介紹下。 一、地理編碼與逆編碼 地理編碼與逆編碼表示的是地名地址與地理坐標(經(jīng)緯度)互相轉換的過程。其中,將地址信息映射為地理坐標的過程稱之為地理編碼;將地理坐標轉換為地址信息的過程稱之為...
閱讀 3319·2021-11-24 09:39
閱讀 3258·2021-10-21 09:38
閱讀 2454·2019-08-29 15:28
閱讀 3797·2019-08-26 12:23
閱讀 2671·2019-08-26 12:19
閱讀 1410·2019-08-23 12:44
閱讀 2178·2019-08-23 12:02
閱讀 1119·2019-08-22 17:05