分析請求
Chrome瀏覽器點擊我的微博首頁之後,右鍵檢查→手機模式→刷新之後url變成
https://m.weibo.cn/p...
→network→XHR篩選
依次對不同類型的內容進行預覽
- 以home開頭的請求:空白框架,無實質內容
- 以config開頭的請求:用於校驗
- 以getIndex開頭的請求:
- 如果請求的url中containerid=100505+id,則內容是網頁設置
- 如果請求的url中containerid=107603+id,則內容是微博內容
- 後一個url中since_id的內容來源於上一次請求返回的內容
- 如果請求的url中containerid=100505+id,則內容是網頁設置
請求一頁的函數
import requests
uid = "3759694794"
headers = {
'Accept': 'application/json, text/plain, */*',
'MWeibo-Pwa': '1',
'Referer':
'https://m.weibo.cn/p/100505'+uid+'/home?from=page_100505_profile&from=page_100505_profile&wvr=6&wvr=6&mod=data&mod=data&is_all=1&is_all=1&jumpfrom=weibocom',
'User-Agent':
'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Mobile Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
'X-XSRF-TOKEN': '3ef4df'
}
baseurl = "https://m.weibo.cn/api/container/getIndex?from[]=page_100505_profile&from[]=page_100505_profile&wvr[]=6&wvr[]=6&mod[]=data&mod[]=data&is_all[]=1&is_all[]=1&jumpfrom=weibocom&containerid=107603"
def get_onepage(url):
try:
response = requests.get(url, headers)
if response.status_code == 200:
return response.json()
except requests.ConnectionError as e:
print('Error:', e.args)
testjson = get_onepage(baseurl+uid)
解析json
## 獲取sinceid
testjson['data']['cardlistInfo']['since_id']
4471262158450663
## 獲取微博總數
testjson['data']['cardlistInfo']['total']
1515
## 獲取微博
import re
for m in testjson['data']['cards']:
if m['card_type'] == 9:
print('創建時間:', m['mblog']['created_at'])
if m['mblog'].get('raw_text') == None:
print('原創內容:', re.sub('<s.*>', '', m['mblog']['text']))
else:
print('轉載內容:', m['mblog']['raw_text'])
創建時間: 昨天 21:20
轉載內容: 哈哈哈//@馬伯庸 :給你看這件事的後續,這兄弟倆的互動實在太……狗了 http://t.cn/A6hrdI0a //@幻想狂劉先生 :段子感也太強了
創建時間: 02-20
轉載內容: //@大偵探家福爾摩斯 :此案無反饋!
創建時間: 02-17
轉載內容: 我也想當助理//@著名網黃貓日 :我的工作之路真的很艱辛
創建時間: 02-15
轉載內容: 轉發微博
創建時間: 02-14
原創內容: 王者榮耀搞到改名卡啦,現在我叫鶴立鱉羣
創建時間: 02-13
轉載內容: [並不簡單]
創建時間: 02-13
轉載內容: //@折翼叢林 :可憐的北紅尾鴝……可以用風油精去膠(千萬別硬撕),接着用洗潔精去風油精,再用溫水去洗潔精,最後用吹風機吹乾。用個大紙箱裝起來讓它休息一下,注意保暖,喂點水,等體力恢復後就放飛(不要拋扔,放在樹枝上就可以了)
創建時間: 02-13
轉載內容: 轉發微博
創建時間: 02-13
轉載內容: [衝鋒隊員]
整合函數
import pandas as pd
def get_weibo(uid, number):
headers = {
'Accept': 'application/json, text/plain, */*',
'MWeibo-Pwa': '1',
'Referer': 'https://m.weibo.cn/p/100505' + uid +
'/home?from=page_100505_profile&from=page_100505_profile&wvr=6&wvr=6&mod=data&mod=data&is_all=1&is_all=1&jumpfrom=weibocom',
'User-Agent':
'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Mobile Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
'X-XSRF-TOKEN': '3ef4df'
}
baseurl = "https://m.weibo.cn/api/container/getIndex?from[]=page_100505_profile&from[]=page_100505_profile&wvr[]=6&wvr[]=6&mod[]=data&mod[]=data&is_all[]=1&is_all[]=1&jumpfrom=weibocom&containerid=107603"+uid
info = get_onepage(baseurl)
tnumber = info['data']['cardlistInfo']['total']
# 確保要爬取的微博數小於等於微博總數
if tnumber < number:
number = tnumber
timelist = []
contentlist = []
baseurl = baseurl + "&since_id="
while len(timelist) < number:
for m in info['data']['cards']:
if m['card_type'] == 9:
timelist.append(m['mblog']['created_at'])
if m['mblog'].get('raw_text') == None:
contentlist.append('原創內容:' + re.sub('<.*>', '', m['mblog']['text']))
else:
contentlist.append('轉載內容:' + m['mblog']['raw_text'])
if info['data']['cardlistInfo'].get('since_id') == None:
break
since_id = info['data']['cardlistInfo']['since_id']
info = get_onepage(baseurl+str(since_id))
output = pd.DataFrame( data={"時間":timelist, "內容":contentlist})
# 解決中文亂碼
output.to_csv( uid+".csv", encoding="utf_8_sig")
get_weibo("3759694794", 30)