經(jīng)過(guò)對(duì)微博品牌的頁(yè)面進(jìn)行分析,數(shù)據(jù)返回一共分為兩種形式马昨,js封裝頁(yè)面代碼渲染在預(yù)加載頁(yè)面中竞帽,鼠標(biāo)向下滑動(dòng)過(guò)程中會(huì)再次請(qǐng)求服務(wù)器,返回json數(shù)據(jù)鸿捧,對(duì)微博內(nèi)容進(jìn)行渲染屹篓,json數(shù)據(jù)一共請(qǐng)求兩次
1.首先對(duì)微博頁(yè)面預(yù)加載數(shù)據(jù)進(jìn)行分析
在這里插入圖片描述
2.然后向下滑動(dòng),繼續(xù)請(qǐng)求json數(shù)據(jù)匙奴,獲取請(qǐng)求鏈接堆巧,拼接請(qǐng)求參數(shù)
在這里插入圖片描述
以下是模擬請(qǐng)求獲取到的結(jié)果示例:
微博內(nèi)容:
在這里插入圖片描述
圖片內(nèi)容:
在這里插入圖片描述
詳細(xì)代碼我上傳到了github, 項(xiàng)目地址
部分代碼如下:
def get_response(self, page):
"""微博每頁(yè)的數(shù)據(jù)分三次請(qǐng)求,初始頁(yè)面為js渲染html恳邀,下拉請(qǐng)求json數(shù)據(jù)渲染懦冰,需要拼接參數(shù)"""
requests.packages.urllib3.disable_warnings()
http = urllib3.PoolManager()
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
'Cookie': self.cookie,
"X-Requested-With": "XMLHttpRequest"
}
start_url = 'https://weibo.com/{profile}?pids=Pl_Official_MyProfileFeed__23&is_search=0&visible=0&is_hot=1&is_tag=0&profile_ftype=1&page={page}&ajaxpagelet=1&ajaxpagelet_v6=1&__ref=%2Fperfectdiary%3Fis_search%3D0%26visible%3D0%26is_hot%3D1%26is_tag%3D0%26profile_ftype%3D1%26page%3D3%23feedtop&_t=FM_157441560856733'.format(
profile=self.profile, page=page)
r = http.request('GET', start_url, headers=headers)
data = json.loads(r.data.decode().strip()[23:-10]).get("html")
soup = BeautifulSoup(data, 'html.parser', from_encoding='utf-8')
result0 = soup.find_all("div", attrs={"action-type": "feed_list_item"})
for pagebar in [0, 1]:
json_url = "https://weibo.com/p/aj/v6/mblog/mbloglist?ajwvr=6&domain=100606&is_search=0&visible=0&is_all=1&is_tag=0&profile_ftype=1&page={page}&pagebar={pagebar}&pl_name=Pl_Official_MyProfileFeed__23&id=1006066020329578&script_uri={script_uri}&feed_type=0&pre_page={pre_page}&domain_op=100606&__rnd=1575859271326".format(
page=page, pagebar=pagebar, pre_page=page, script_uri=self.script_uri)
res = http.request('GET', json_url, headers=headers)
json_data = json.loads(res.data.decode().strip()).get("data")
json_soup = BeautifulSoup(json_data, 'html.parser', from_encoding='utf-8')
result0 += json_soup.find_all("div", attrs={"action-type": "feed_list_item"})
return result0
Ps:需要注意的一點(diǎn),微博默認(rèn)返回的圖片是縮略圖谣沸,清晰圖不高刷钢,想要獲取到高清大圖,需要解析到微博大圖的地址乳附,我在代碼中處理了此類(lèi)問(wèn)題内地,替換了url的地址,以方便獲取高清大圖
在這里插入圖片描述