新聞中心
事情是這樣的,由于昨天上班忘帶筆記本了,就臨時(shí)用了一下同事王大國(guó)的筆記本電腦,中間偶然登了一下百度地圖開放平臺(tái)的賬號(hào),沒想到他今天給我發(fā)來(lái)這個(gè):

尷尬的想找個(gè)地縫鉆進(jìn)去
經(jīng)過一番詢問,他終于道出了實(shí)情,原來(lái)百度的賬號(hào)會(huì)自動(dòng)同步。
例如,在登錄了網(wǎng)頁(yè)版的百度地圖、百度API等賬號(hào)后,瀏覽器會(huì)自動(dòng)保持,你的所有百度賬號(hào)就自動(dòng)登上了。
當(dāng)你使用已登錄百度賬號(hào)手機(jī)或電腦的百度搜索框時(shí),他那里就會(huì)自動(dòng)彈出歷史搜索記錄,如下圖:
然后可以用python爬蟲定時(shí)獲取搜索記錄
首先抓包獲取數(shù)據(jù)接口:
然后寫個(gè)小爬蟲,因?yàn)橐匈~號(hào)信息,所以要帶上cookie:
- import requests
- header={
- 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0',
- "Cookie":'',
- url='https://www.baidu.com/sugrec?prod=pc_his&from=pc_web&json=1'
- response=requests.get(url,headers=header)
- print(response.text)
- UnicodeEncodeError: 'latin-1' codec can't encode character '\u2026' in position 518: ordinal not in range(256)
但是你可能會(huì)遇到上面這種情況,報(bào)編碼錯(cuò)誤
我去網(wǎng)上查了一下,以為是編碼的問題,然后給cookie加了“utf-8”編碼方式,如下:
- import requests
- header={
- 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0',
- "Cookie":''.encode("utf-8"),
- url='https://www.baidu.com/sugrec?prod=pc_his&from=pc_web&json=1'
- response=requests.get(url,headers=header)
- print(response.text)
- {"err_no":0,"errmsg":"","queryid":"0x21a1c8a90872b8"}
又報(bào)錯(cuò)了。。。。。
就在我認(rèn)為百度是不是有什么高端的反爬措施時(shí),突然發(fā)現(xiàn)cookie的“BDUSS”參數(shù)有點(diǎn)問題,如下:
- BDUSS=JkRjIyUFR2T01Yd3QxcTZ…AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP4Gzl~-Bs5fZX
中間多了省略號(hào),這是因?yàn)樽址L(zhǎng)了,被自動(dòng)省略了,于是我趕緊把該參數(shù)補(bǔ)全,重新嘗試了一下:
- import requests
- header={
- 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0',
- "Cookie":'',
- }
- url='https://www.baidu.com/sugrec?prod=pc_his&from=pc_web&json=1'
- response=requests.get(url,headers=header)
- print(response.text)
大功告成
最后加個(gè)循環(huán)程序:
- import requests
- import json
- import datetime,time
- header={
- 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0',
- "Cookie":'',
- }
- url='https://www.baidu.com/sugrec?prod=pc_his&from=pc_web&json=1'
- result=[]
- while True:
- dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') #時(shí)間戳
- response=requests.get(url,headers=header)
- datas=json.loads(response.text)['g']
- for data in datas:
- if data['q'] not in result:
- print(data['q']+' '+dt)
- result.append(data['q'])
- time.sleep(60)
以自己的親身經(jīng)歷告訴大家,千萬(wàn)不要在被人電腦上亂登賬號(hào),小則丟人、大則丟金,切記切記!
當(dāng)前標(biāo)題:用同事電腦登了一次百度賬號(hào),搜索框就被他用Python監(jiān)控了
本文地址:http://www.5511xx.com/article/cdhogog.html


咨詢
建站咨詢
