Python?爬取微博熱搜頁面
前期準(zhǔn)備:
fiddler 抓包工具
Python3.6
谷歌瀏覽器
分析:
1.清理瀏覽器緩存cookie以至于看到整個(gè)請(qǐng)求過程,因?yàn)镻ython代碼開始請(qǐng)求的時(shí)候不帶任何緩存。
2.不考慮過多的header參數(shù),先請(qǐng)求一次,看看返回結(jié)果

圖中第一個(gè)鏈接是無緩存cookie直接訪問的,狀態(tài)碼為302進(jìn)行了重定向,用返回值.url會(huì)得到該url后面會(huì)用到(headers里的Referer參數(shù)值)
2 ,3 鏈接沒有用太大用處為第 4 個(gè)鏈接做鋪墊但是都可以用固定參數(shù)可以不用訪問

訪問https://passport.weibo.com/visitor/genvisitor ,cookie為tid=__095,注意tid需要去掉轉(zhuǎn)義字符‘\’,get傳的參數(shù)有用的只有t也就是tid 其他都是固定值 、_rand是浮點(diǎn)隨機(jī)數(shù)沒啥具體的意義可以用Python的random.random()函數(shù),需要導(dǎo)入random庫(kù),get傳參使用params=,post傳參用data=,不是隨便都能用的

得到返回值 含有SUB 和SUBP參數(shù)的值
正好是訪問最后一個(gè)鏈接也就是熱搜榜需要的cookie的值
到此分析結(jié)束
代碼:
import requests
import random
import re
import urllib3
#警告忽略
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class Wb():
? ? def __init__(self):
? ? ?? ?#利用session保持回話
? ? ? ? self.session=requests.Session()
? ? ? ? #清理headers字典,不然update好像不會(huì)起作用
? ? ? ? self.session.headers.clear()
? ? ? ? self.header={
? ? ? ? ? ? "Host": "weibo.com",
? ? ? ? ? ? "Connection": "keep-alive",
? ? ? ? ? ? "Upgrade-Insecure-Requests": "1",
? ? ? ? ? ? "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) "
? ? ? ? ? ? ? ? ? ? ? ? ? "AppleWebKit/537.36 (KHTML, like Gecko) "
? ? ? ? ? ? ? ? ? ? ? ? ? "Chrome/86.0.4240.198 Safari/537.36",
? ? ? ? ? ? "Accept": "text/html,application/xhtml+xml,application/xml;"
? ? ? ? ? ? ? ? ? ? ? "q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,"
? ? ? ? ? ? ? ? ? ? ? "application/signed-exchange;v=b3;q=0.9",
? ? ? ? ? ? "Sec-Fetch-Site": "cross-site",
? ? ? ? ? ? "Sec-Fetch-Mode": "navigate",
? ? ? ? ? ? "Sec-Fetch-Dest": "document",
? ? ? ? ? ? "Accept-Encoding": "gzip, deflate, br",
? ? ? ? ? ? "Accept-Language": "zh-CN,zh;q=0.9",
? ? ? ? }
? ? ? ? #設(shè)置代理如果需要fiddler抓包分析代碼提交的參數(shù)使用下面代理如果不使用選擇下面的代碼self.fiddler_proxies=None
? ? ? ? self.fiddler_proxies = {'http': 'http://127.0.0.1:8888', 'https': 'http://127.0.0.1:8888'}
? ? ? ? # self.fiddler_proxies=None
? ? def get_top_summary(self):
? ? ?? ?#更新添加header ? headers.update只會(huì)覆蓋相同鍵值的值不會(huì)覆蓋全部
? ? ? ? self.session.headers.update(self.header)
? ? ? ? #verify=False 不檢查證書
? ? ? ? response=self.session.get(url="https://weibo.com/",proxies=self.fiddler_proxies,verify=False)
? ? ? ? print(response.url)
? ? ? ? response.encoding='gbk'
? ? ? ? data1={
? ? ? ? ? ? "cb":"gen_callback",
? ? ? ? ? ? "fp":'{"os":"1","browser":"Chrome86,0,4240,198",'
? ? ? ? ? ? ? ? ?'"fonts":"undefined","screenInfo":"1920*1080*24",'
? ? ? ? ? ? ? ? ?'"plugins":"Portable Document Format::internal-pdf-viewer::'
? ? ? ? ? ? ? ? ?'Chromium PDF Plugin|::mhjfbmdgcfjbbpaeojofohoefgiehjai::'
? ? ? ? ? ? ? ? ?'Chromium PDF Viewer|::internal-nacl-plugin::Native Client"}'
? ? ? ? }
? ? ? ? header1={
? ? ? ? ? ? "Host": "passport.weibo.com",
? ? ? ? ? ? "Cache-Control": "max-age=0",
? ? ? ? ? ? "If-Modified-Since": "0",
? ? ? ? ? ? "Content-Type": "application/x-www-form-urlencoded",
? ? ? ? ? ? "Accept": "*/*",
? ? ? ? ? ? "Origin": "https://passport.weibo.com",
? ? ? ? ? ? "Sec-Fetch-Site": "same-origin",
? ? ? ? ? ? "Sec-Fetch-Mode": "cors",
? ? ? ? ? ? "Sec-Fetch-Dest": "empty",
? ? ? ? ? ? "Referer": response.url,
? ? ? ? }
? ? ? ? self.session.headers.update(header1)
? ? ? ? response1=self.session.post(url="https://passport.weibo.com/visitor/genvisitor"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ,data=data1,proxies=self.fiddler_proxies,verify=False)
? ? ? ? #利用正則表達(dá)式解析tid參數(shù)的值
? ? ? ? t=re.search('{"tid":"(.*)","new_tid"',response1.text).groups()[0]
? ? ? ? data2={
? ? ? ? ? ? "a":"incarnate",
? ? ? ? ? ? "t":t.replace("\\",""),
? ? ? ? ? ? "w": "2",
? ? ? ? ? ? "c": "095",
? ? ? ? ? ? "gc":"",
? ? ? ? ? ? "cb":"cross_domain",
? ? ? ? ? ? "from":"weibo",
? ? ? ? ? ? "_rand":random.random()
? ? ? ? }
? ? ? ? header2={
? ? ? ? ? ? "Sec-Fetch-Mode": "no-cors",
? ? ? ? ? ? "Sec-Fetch-Dest": "script",
? ? ? ? ? ? "Cookie":"tid="+t.replace("\\","")+"__095"
? ? ? ? }
? ? ? ? self.session.headers.update(header2)
? ? ? ? response2 = self.session.get(url="https://passport.weibo.com/visitor/visitor",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?params=data2,proxies=self.fiddler_proxies,verify=False)
? ? ? ? #從返回值中獲取cookie字典
? ? ? ? cookie = requests.utils.dict_from_cookiejar(response2.cookies)
? ? ? ? header3={
? ? ? ? ? ? "Cookie":"SUB="+cookie["SUB"]+";"+"SUBP="+cookie["SUBP"],
? ? ? ? ? ? "Host": "s.weibo.com",
? ? ? ? ? ? "Upgrade-Insecure-Requests": "1"
? ? ? ? }
? ? ? ? self.session.headers.update(header3)
? ? ? ? response3=self.session.get(url="https://s.weibo.com/top/summary",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?proxies=self.fiddler_proxies,verify=False)
? ? ? ? # print(response3.text)
if __name__ == '__main__':
? ? wb=Wb()
? ? wb.get_top_summary()至此只能得到原始的html頁面,想要進(jìn)一步操作需要在HTML里面提取有用的數(shù)據(jù)。。。。。。
爬蟲初期需要更多的是耐心
到此這篇關(guān)于Python 爬取微博熱搜頁面的文章就介紹到這了,更多相關(guān)Python 爬取微博熱搜頁面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用python實(shí)現(xiàn)將數(shù)組元素按從小到大的順序排列方法
今天小編就為大家分享一篇用python實(shí)現(xiàn)將數(shù)組元素按從小到大的順序排列方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07
python?實(shí)時(shí)獲取kafka消費(fèi)隊(duì)列信息示例詳解
這篇文章主要介紹了python實(shí)時(shí)獲取kafka消費(fèi)隊(duì)列信息,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
Python使用sftp實(shí)現(xiàn)上傳和下載功能
這篇文章主要為大家詳細(xì)介紹了Python使用sftp實(shí)現(xiàn)上傳和下載功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
Sklearn多種算法實(shí)現(xiàn)人臉補(bǔ)全的項(xiàng)目實(shí)踐
本文主要介紹了Sklearn多種算法實(shí)現(xiàn)人臉補(bǔ)全的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Python學(xué)生成績(jī)管理系統(tǒng)簡(jiǎn)潔版
這篇文章主要為大家詳細(xì)介紹了簡(jiǎn)潔版的Python學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Object arrays cannot be loaded when
這篇文章主要介紹了Object arrays cannot be loaded when allow_pickle=False,本文給大家分享問題解決思路,需要的朋友可以參考下2022-11-11

