爬取某人的微博信息

 1 import urllib.request
 2 import json
 3 
 4 #定義要爬取的微博大V的微博ID
 5 id='3995218983'
 6 
 7 #設置代理IP
 8 proxy_addr="122.241.72.191:808"
 9 
10 #定義頁面打開函數
11 def use_proxy(url,proxy_addr):
12     req=urllib.request.Request(url)
13     req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0")
14     proxy=urllib.request.ProxyHandler({'http':proxy_addr})
15     opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
16     urllib.request.install_opener(opener)
17     data=urllib.request.urlopen(req).read().decode('utf-8','ignore')
18     return data
19 
20 #獲取微博主頁的containerid,爬取微博內容時須要此id
21 def get_containerid(url):
22     data=use_proxy(url,proxy_addr)
23     content=json.loads(data).get('data')
24     for data in content.get('tabsInfo').get('tabs'):
25         if(data.get('tab_type')=='weibo'):
26             containerid=data.get('containerid')
27     return containerid
28 
29 #獲取微博大V帳號的用戶基本信息,如:微博暱稱、微博地址、微博頭像、關注人數、粉絲數、性別、等級等
30 def get_userInfo(id):
31     url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id
32     data=use_proxy(url,proxy_addr)
33     content=json.loads(data).get('data')
34     profile_image_url=content.get('userInfo').get('profile_image_url')
35     description=content.get('userInfo').get('description')
36     profile_url=content.get('userInfo').get('profile_url')
37     verified=content.get('userInfo').get('verified')
38     guanzhu=content.get('userInfo').get('follow_count')
39     name=content.get('userInfo').get('screen_name')
40     fensi=content.get('userInfo').get('followers_count')
41     gender=content.get('userInfo').get('gender')
42     urank=content.get('userInfo').get('urank')
43     print("微博暱稱:"+name+"\n"+"微博主頁地址:"+profile_url+"\n"+"微博頭像地址:"+profile_image_url+"\n"+"是否定證:"+str(verified)+"\n"+"微博說明:"+description+"\n"+"關注人數:"+str(guanzhu)+"\n"+"粉絲數:"+str(fensi)+"\n"+"性別:"+gender+"\n"+"微博等級:"+str(urank)+"\n")
44 
45 
46 #獲取微博內容信息,並保存到文本中,內容包括:每條微博的內容、微博詳情頁面地址、點贊數、評論數、轉發數等
47 def get_weibo(id,file):
48     i=1
49     while True:
50         url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id
51         weibo_url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id+'&containerid='+get_containerid(url)+'&page='+str(i)
52         try:
53             data=use_proxy(weibo_url,proxy_addr)
54             content=json.loads(data).get('data')
55             cards=content.get('cards')
56             if(len(cards)>0):
57                 for j in range(len(cards)):
58                     print("-----正在爬取第"+str(i)+"頁,第"+str(j)+"條微博------")
59                     card_type=cards[j].get('card_type')
60                     if(card_type==9):
61                         mblog=cards[j].get('mblog')
62                         attitudes_count=mblog.get('attitudes_count')
63                         comments_count=mblog.get('comments_count')
64                         created_at=mblog.get('created_at')
65                         reposts_count=mblog.get('reposts_count')
66                         scheme=cards[j].get('scheme')
67                         text=mblog.get('text')
68                         with open(file,'a',encoding='utf-8') as fh:
69                             fh.write("----第"+str(i)+"頁,第"+str(j)+"條微博----"+"\n")
70                             fh.write("微博地址:"+str(scheme)+"\n"+"發佈時間:"+str(created_at)+"\n"+"微博內容:"+text+"\n"+"點贊數:"+str(attitudes_count)+"\n"+"評論數:"+str(comments_count)+"\n"+"轉發數:"+str(reposts_count)+"\n")
71                 i+=1
72             else:
73                 break
74         except Exception as e:
75             print(e)
76             pass
77 
78 if __name__=="__main__":
79     file=id+".txt"
80     get_userInfo(id)
81     get_weibo(id,file)
相關文章
相關標籤/搜索