Python爬蟲爬取新浪微博內(nèi)容示例【基于代理IP】
本文實(shí)例講述了Python爬蟲爬取新浪微博內(nèi)容。分享給大家供大家參考,具體如下:
用Python編寫爬蟲,爬取微博大V的微博內(nèi)容,本文以女神的微博為例(爬新浪m站:https://m.weibo.cn/u/1259110474)
一般做爬蟲爬取網(wǎng)站,首選的都是m站,其次是wap站,最后考慮PC站。當(dāng)然,這不是絕對(duì)的,有的時(shí)候PC站的信息最全,而你又恰好需要全部的信息,那么PC站是你的首選。一般m站都以m開頭后接域名, 所以本文開搞的網(wǎng)址就是 m.weibo.cn。
前期準(zhǔn)備
1.代理IP
網(wǎng)上有很多免費(fèi)代理ip,如西刺免費(fèi)代理IPhttp://www.xicidaili.com/,自己可找一個(gè)可以使用的進(jìn)行測(cè)試;
2.抓包分析
通過(guò)抓包獲取微博內(nèi)容地址,這里不再細(xì)說(shuō),不明白的小伙伴可以自行百度查找相關(guān)資料,下面直接上完整的代碼
完整代碼:
# -*- coding: utf-8 -*- import urllib.request import json #定義要爬取的微博大V的微博ID id='1259110474' #設(shè)置代理IP proxy_addr="122.241.72.191:808" #定義頁(yè)面打開函數(shù) def use_proxy(url,proxy_addr): req=urllib.request.Request(url) 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") proxy=urllib.request.ProxyHandler({'http':proxy_addr}) opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler) urllib.request.install_opener(opener) data=urllib.request.urlopen(req).read().decode('utf-8','ignore') return data #獲取微博主頁(yè)的containerid,爬取微博內(nèi)容時(shí)需要此id def get_containerid(url): data=use_proxy(url,proxy_addr) content=json.loads(data).get('data') for data in content.get('tabsInfo').get('tabs'): if(data.get('tab_type')=='weibo'): containerid=data.get('containerid') return containerid #獲取微博大V賬號(hào)的用戶基本信息,如:微博昵稱、微博地址、微博頭像、關(guān)注人數(shù)、粉絲數(shù)、性別、等級(jí)等 def get_userInfo(id): url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id data=use_proxy(url,proxy_addr) content=json.loads(data).get('data') profile_image_url=content.get('userInfo').get('profile_image_url') description=content.get('userInfo').get('description') profile_url=content.get('userInfo').get('profile_url') verified=content.get('userInfo').get('verified') guanzhu=content.get('userInfo').get('follow_count') name=content.get('userInfo').get('screen_name') fensi=content.get('userInfo').get('followers_count') gender=content.get('userInfo').get('gender') urank=content.get('userInfo').get('urank') print("微博昵稱:"+name+"\n"+"微博主頁(yè)地址:"+profile_url+"\n"+"微博頭像地址:"+profile_image_url+"\n"+"是否認(rèn)證:"+str(verified)+"\n"+"微博說(shuō)明:"+description+"\n"+"關(guān)注人數(shù):"+str(guanzhu)+"\n"+"粉絲數(shù):"+str(fensi)+"\n"+"性別:"+gender+"\n"+"微博等級(jí):"+str(urank)+"\n") #獲取微博內(nèi)容信息,并保存到文本中,內(nèi)容包括:每條微博的內(nèi)容、微博詳情頁(yè)面地址、點(diǎn)贊數(shù)、評(píng)論數(shù)、轉(zhuǎn)發(fā)數(shù)等 def get_weibo(id,file): i=1 while True: url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id weibo_url='https://m.weibo.cn/api/container/getIndex?type=uid&value='+id+'&containerid='+get_containerid(url)+'&page='+str(i) try: data=use_proxy(weibo_url,proxy_addr) content=json.loads(data).get('data') cards=content.get('cards') if(len(cards)>0): for j in range(len(cards)): print("-----正在爬取第"+str(i)+"頁(yè),第"+str(j)+"條微博------") card_type=cards[j].get('card_type') if(card_type==9): mblog=cards[j].get('mblog') attitudes_count=mblog.get('attitudes_count') comments_count=mblog.get('comments_count') created_at=mblog.get('created_at') reposts_count=mblog.get('reposts_count') scheme=cards[j].get('scheme') text=mblog.get('text') with open(file,'a',encoding='utf-8') as fh: fh.write("----第"+str(i)+"頁(yè),第"+str(j)+"條微博----"+"\n") fh.write("微博地址:"+str(scheme)+"\n"+"發(fā)布時(shí)間:"+str(created_at)+"\n"+"微博內(nèi)容:"+text+"\n"+"點(diǎn)贊數(shù):"+str(attitudes_count)+"\n"+"評(píng)論數(shù):"+str(comments_count)+"\n"+"轉(zhuǎn)發(fā)數(shù):"+str(reposts_count)+"\n") i+=1 else: break except Exception as e: print(e) pass if __name__=="__main__": file=id+".txt" get_userInfo(id) get_weibo(id,file)
爬取結(jié)果
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python爬蟲爬取指定內(nèi)容的解決方法
- python爬蟲爬取bilibili網(wǎng)頁(yè)基本內(nèi)容
- Python爬蟲爬取百度搜索內(nèi)容代碼實(shí)例
- python爬蟲開發(fā)之使用python爬蟲庫(kù)requests,urllib與今日頭條搜索功能爬取搜索內(nèi)容實(shí)例
- python爬取內(nèi)容存入Excel實(shí)例
- python爬取網(wǎng)頁(yè)內(nèi)容轉(zhuǎn)換為PDF文件
- Python下使用Scrapy爬取網(wǎng)頁(yè)內(nèi)容的實(shí)例
- 基于Python實(shí)現(xiàn)web網(wǎng)頁(yè)內(nèi)容爬取的方法
相關(guān)文章
python函數(shù)map()和partial()的知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享了關(guān)于python函數(shù)map()和partial()的知識(shí)點(diǎn)總結(jié),需要的朋友們可以參考下。2020-05-05Python中Selenium模擬JQuery滑動(dòng)解鎖實(shí)例
這篇文章主要介紹了Python中Selenium模擬JQuery滑動(dòng)解鎖實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07flask循環(huán)導(dǎo)入的問(wèn)題解決
循環(huán)導(dǎo)入是指兩個(gè)文件相互導(dǎo)入對(duì),本文主要介紹了flask循環(huán)導(dǎo)入的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04基于Python中isfile函數(shù)和isdir函數(shù)使用詳解
今天小編就為大家分享一篇基于Python中isfile函數(shù)和isdir函數(shù)使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11Python實(shí)現(xiàn)克里金插值法的過(guò)程詳解
克里金算法提供的半變異函數(shù)模型有高斯、線形、球形、阻尼正弦和指數(shù)模型等,在對(duì)氣象要素場(chǎng)插值時(shí)球形模擬比較好。本文將用Python實(shí)現(xiàn)克里金插值法,感興趣的可以了解一下2022-11-11手把手教你打造個(gè)性化全棧應(yīng)用Python?Reflex框架全面攻略
Reflex框架是為了解決傳統(tǒng)全棧開發(fā)中的一些挑戰(zhàn)而誕生的,它充分利用了現(xiàn)代前端框架(如React)的優(yōu)勢(shì),與后端技術(shù)(如Node.js)深度集成,使得開發(fā)者能夠更加流暢地構(gòu)建整個(gè)應(yīng)用,Reflex的設(shè)計(jì)理念包括簡(jiǎn)化、響應(yīng)性和一致性,旨在提高全棧開發(fā)的效率和可維護(hù)性2023-12-12python正則表達(dá)式查找和替換內(nèi)容的實(shí)例詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python正則表達(dá)式查找和替換內(nèi)容的實(shí)例詳解內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)參考下。2021-10-10python 圖像增強(qiáng)算法實(shí)現(xiàn)詳解
這篇文章主要介紹了python 圖像增強(qiáng)算法實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01pygame實(shí)現(xiàn)簡(jiǎn)單五子棋游戲
這篇文章主要為大家詳細(xì)介紹了pygame實(shí)現(xiàn)簡(jiǎn)單五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下<BR>2022-01-01Python+MediaPipe實(shí)現(xiàn)檢測(cè)人臉功能詳解
MediaPipe是用于構(gòu)建多模態(tài)(例如視頻、音頻或任何時(shí)間序列數(shù)據(jù))、跨平臺(tái)(即eAndroid、IOS、web、邊緣設(shè)備)應(yīng)用ML管道的框架。本文將利用MediaPipe實(shí)現(xiàn)檢測(cè)人臉功能,需要的可以參考一下2022-02-02