經(jīng)過對微博品牌的頁面進(jìn)行分析,數(shù)據(jù)返回一共分為兩種形式,js封裝頁面代碼渲染在預(yù)加載頁面中,鼠標(biāo)向下滑動過程中會再次請求服務(wù)器,返回json數(shù)據(jù),對微博內(nèi)容進(jìn)行渲染,json數(shù)據(jù)一共請求兩次
1.首先對微博頁面預(yù)加載數(shù)據(jù)進(jìn)行分析
在這里插入圖片描述
2.然后向下滑動,繼續(xù)請求json數(shù)據(jù),獲取請求鏈接,拼接請求參數(shù)
在這里插入圖片描述
以下是模擬請求獲取到的結(jié)果示例:
微博內(nèi)容:
在這里插入圖片描述
圖片內(nèi)容:
在這里插入圖片描述
詳細(xì)代碼我上傳到了github, 項目地址
部分代碼如下:
def get_response(self, page):
"""微博每頁的數(shù)據(jù)分三次請求,初始頁面為js渲染html,下拉請求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:需要注意的一點,微博默認(rèn)返回的圖片是縮略圖,清晰圖不高,想要獲取到高清大圖,需要解析到微博大圖的地址,我在代碼中處理了此類問題,替換了url的地址,以方便獲取高清大圖
在這里插入圖片描述