生活随笔
收集整理的這篇文章主要介紹了
[Python]网络爬虫(九):百度贴吧的网络爬虫(v0.4)源码及解析
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
更新:感謝評(píng)論中朋友的提醒,百度貼吧現(xiàn)在已經(jīng)改成utf-8編碼了吧,需要把代碼中的decode('gbk')改成decode('utf-8')。
百度貼吧的爬蟲(chóng)制作和糗百的爬蟲(chóng)制作原理基本相同,都是通過(guò)查看源碼扣出關(guān)鍵數(shù)據(jù),然后將其存儲(chǔ)到本地txt文件。
源碼下載:
http://download.csdn.net/detail/wxg694175346/6925583
項(xiàng)目?jī)?nèi)容:
用Python寫(xiě)的百度貼吧的網(wǎng)絡(luò)爬蟲(chóng)。
使用方法:
新建一個(gè)BugBaidu.py文件,然后將代碼復(fù)制到里面后,雙擊運(yùn)行。
程序功能:
將貼吧中樓主發(fā)布的內(nèi)容打包txt存儲(chǔ)到本地。
原理解釋:
首先,先瀏覽一下某一條貼吧,點(diǎn)擊只看樓主并點(diǎn)擊第二頁(yè)之后url發(fā)生了一點(diǎn)變化,變成了:
http://tieba.baidu.com/p/2296712428?see_lz=1&pn=1
可以看出來(lái),see_lz=1是只看樓主,pn=1是對(duì)應(yīng)的頁(yè)碼,記住這一點(diǎn)為以后的編寫(xiě)做準(zhǔn)備。
這就是我們需要利用的url。
接下來(lái)就是查看頁(yè)面源碼。
首先把題目摳出來(lái)存儲(chǔ)文件的時(shí)候會(huì)用到。
可以看到百度使用gbk編碼,標(biāo)題使用h1標(biāo)記:
[html]?view plaincopy
<h1?class="core_title_txt"?title="【原創(chuàng)】時(shí)尚首席(關(guān)于時(shí)尚,名利,事業(yè),愛(ài)情,勵(lì)志)">【原創(chuàng)】時(shí)尚首席(關(guān)于時(shí)尚,名利,事業(yè),愛(ài)情,勵(lì)志)</h1>??
同樣,正文部分用div和class綜合標(biāo)記,接下來(lái)要做的只是用正則表達(dá)式來(lái)匹配即可。
運(yùn)行截圖:
生成的txt文件:
[python]?view plaincopy
?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? import?string?? import?urllib2?? import?re?? ?? ?? class?HTML_Tool:?? ?????? ????BgnCharToNoneRex?=?re.compile("(\t|\n|?|<a.*?>|<img.*?>)")?? ?????? ?????? ????EndCharToNoneRex?=?re.compile("<.*?>")?? ?? ?????? ????BgnPartRex?=?re.compile("<p.*?>")?? ????CharToNewLineRex?=?re.compile("(<br/>|</p>|<tr>|<div>|</div>)")?? ????CharToNextTabRex?=?re.compile("<td>")?? ?? ?????? ????replaceTab?=?[("<","<"),(">",">"),("&","&"),("&","\""),("?","?")]?? ?????? ????def?Replace_Char(self,x):?? ????????x?=?self.BgnCharToNoneRex.sub("",x)?? ????????x?=?self.BgnPartRex.sub("\n????",x)?? ????????x?=?self.CharToNewLineRex.sub("\n",x)?? ????????x?=?self.CharToNextTabRex.sub("\t",x)?? ????????x?=?self.EndCharToNoneRex.sub("",x)?? ?? ????????for?t?in?self.replaceTab:???? ????????????x?=?x.replace(t[0],t[1])???? ????????return?x???? ?????? class?Baidu_Spider:?? ?????? ????def?__init__(self,url):???? ????????self.myUrl?=?url?+?'?see_lz=1'?? ????????self.datas?=?[]?? ????????self.myTool?=?HTML_Tool()?? ????????print?u'已經(jīng)啟動(dòng)百度貼吧爬蟲(chóng),咔嚓咔嚓'?? ???? ?????? ????def?baidu_tieba(self):?? ?????????? ????????myPage?=?urllib2.urlopen(self.myUrl).read().decode("gbk")?? ?????????? ????????endPage?=?self.page_counter(myPage)?? ?????????? ????????title?=?self.find_title(myPage)?? ????????print?u'文章名稱:'?+?title?? ?????????? ????????self.save_data(self.myUrl,title,endPage)?? ?? ?????? ????def?page_counter(self,myPage):?? ?????????? ????????myMatch?=?re.search(r'class="red">(\d+?)</span>',?myPage,?re.S)?? ????????if?myMatch:???? ????????????endPage?=?int(myMatch.group(1))?? ????????????print?u'爬蟲(chóng)報(bào)告:發(fā)現(xiàn)樓主共有%d頁(yè)的原創(chuàng)內(nèi)容'?%?endPage?? ????????else:?? ????????????endPage?=?0?? ????????????print?u'爬蟲(chóng)報(bào)告:無(wú)法計(jì)算樓主發(fā)布內(nèi)容有多少頁(yè)!'?? ????????return?endPage?? ?? ?????? ????def?find_title(self,myPage):?? ?????????? ????????myMatch?=?re.search(r'<h1.*?>(.*?)</h1>',?myPage,?re.S)?? ????????title?=?u'暫無(wú)標(biāo)題'?? ????????if?myMatch:?? ????????????title??=?myMatch.group(1)?? ????????else:?? ????????????print?u'爬蟲(chóng)報(bào)告:無(wú)法加載文章標(biāo)題!'?? ?????????? ????????title?=?title.replace('\\','').replace('/','').replace(':','').replace('*','').replace('?','').replace('"','').replace('>','').replace('<','').replace('|','')?? ????????return?title?? ?? ?? ?????? ????def?save_data(self,url,title,endPage):?? ?????????? ????????self.get_data(url,endPage)?? ?????????? ????????f?=?open(title+'.txt','w+')?? ????????f.writelines(self.datas)?? ????????f.close()?? ????????print?u'爬蟲(chóng)報(bào)告:文件已下載到本地并打包成txt文件'?? ????????print?u'請(qǐng)按任意鍵退出...'?? ????????raw_input();?? ?? ?????? ????def?get_data(self,url,endPage):?? ????????url?=?url?+?'&pn='?? ????????for?i?in?range(1,endPage+1):?? ????????????print?u'爬蟲(chóng)報(bào)告:爬蟲(chóng)%d號(hào)正在加載中...'?%?i?? ????????????myPage?=?urllib2.urlopen(url?+?str(i)).read()?? ?????????????? ????????????self.deal_data(myPage.decode('gbk'))?? ?????????????? ?? ?????? ????def?deal_data(self,myPage):?? ????????myItems?=?re.findall('id="post_content.*?>(.*?)</div>',myPage,re.S)?? ????????for?item?in?myItems:?? ????????????data?=?self.myTool.Replace_Char(item.replace("\n","").encode('gbk'))?? ????????????self.datas.append(data+'\n')?? ?? ?? ?? ?? print?u? ? ? ? ? ? ? ? ? ?? ?? ?? ?? ?? print?u'請(qǐng)輸入貼吧的地址最后的數(shù)字串:'?? bdurl?=?'http://tieba.baidu.com/p/'?+?str(raw_input(u'http://tieba.baidu.com/p/'))??? ?? ?? mySpider?=?Baidu_Spider(bdurl)?? mySpider.baidu_tieba()??
總結(jié)
以上是生活随笔為你收集整理的[Python]网络爬虫(九):百度贴吧的网络爬虫(v0.4)源码及解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。