生活随笔
收集整理的這篇文章主要介紹了
[Python]网络爬虫(九):百度贴吧的网络爬虫(v0.4)源码及解析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
更新:感謝評論中朋友的提醒,百度貼吧現在已經改成utf-8編碼了吧,需要把代碼中的decode('gbk')改成decode('utf-8')。
百度貼吧的爬蟲制作和糗百的爬蟲制作原理基本相同,都是通過查看源碼扣出關鍵數據,然后將其存儲到本地txt文件。
源碼下載:
http://download.csdn.net/detail/wxg694175346/6925583
項目內容:
用Python寫的百度貼吧的網絡爬蟲。
使用方法:
新建一個BugBaidu.py文件,然后將代碼復制到里面后,雙擊運行。
程序功能:
將貼吧中樓主發布的內容打包txt存儲到本地。
原理解釋:
首先,先瀏覽一下某一條貼吧,點擊只看樓主并點擊第二頁之后url發生了一點變化,變成了:
http://tieba.baidu.com/p/2296712428?see_lz=1&pn=1
可以看出來,see_lz=1是只看樓主,pn=1是對應的頁碼,記住這一點為以后的編寫做準備。
這就是我們需要利用的url。
接下來就是查看頁面源碼。
首先把題目摳出來存儲文件的時候會用到。
可以看到百度使用gbk編碼,標題使用h1標記:
[html]?view plaincopy
<h1?class="core_title_txt"?title="【原創】時尚首席(關于時尚,名利,事業,愛情,勵志)">【原創】時尚首席(關于時尚,名利,事業,愛情,勵志)</h1>??
同樣,正文部分用div和class綜合標記,接下來要做的只是用正則表達式來匹配即可。
運行截圖:
生成的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'已經啟動百度貼吧爬蟲,咔嚓咔嚓'?? ???? ?????? ????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'爬蟲報告:發現樓主共有%d頁的原創內容'?%?endPage?? ????????else:?? ????????????endPage?=?0?? ????????????print?u'爬蟲報告:無法計算樓主發布內容有多少頁!'?? ????????return?endPage?? ?? ?????? ????def?find_title(self,myPage):?? ?????????? ????????myMatch?=?re.search(r'<h1.*?>(.*?)</h1>',?myPage,?re.S)?? ????????title?=?u'暫無標題'?? ????????if?myMatch:?? ????????????title??=?myMatch.group(1)?? ????????else:?? ????????????print?u'爬蟲報告:無法加載文章標題!'?? ?????????? ????????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'爬蟲報告:文件已下載到本地并打包成txt文件'?? ????????print?u'請按任意鍵退出...'?? ????????raw_input();?? ?? ?????? ????def?get_data(self,url,endPage):?? ????????url?=?url?+?'&pn='?? ????????for?i?in?range(1,endPage+1):?? ????????????print?u'爬蟲報告:爬蟲%d號正在加載中...'?%?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'請輸入貼吧的地址最后的數字串:'?? bdurl?=?'http://tieba.baidu.com/p/'?+?str(raw_input(u'http://tieba.baidu.com/p/'))??? ?? ?? mySpider?=?Baidu_Spider(bdurl)?? mySpider.baidu_tieba()??
總結
以上是生活随笔為你收集整理的[Python]网络爬虫(九):百度贴吧的网络爬虫(v0.4)源码及解析的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。