資源簡介
包含了糗事百科python爬蟲 百度貼吧爬蟲 和360新聞爬蟲樣例 歡迎下載

代碼片段和文件信息
#?-*-?coding:?utf-8?-*-??
#---------------------------------------??
#???程序:360新聞標(biāo)題獲取器??
#???版本:0.5??
#???作者:閑云??
#???日期:2013-11-03??
#???語言:Python?2.7????
#???功能:將360新聞標(biāo)題的內(nèi)容打包txt存儲到本地。??
#---------------------------------------??
???
import?string??
import?urllib2??
import?re??
??
#-----------?處理頁面上的各種標(biāo)簽?-----------??
class?HTML_Tool:??
????#?用非?貪婪模式?匹配?\t?或者?\n?或者?空格?或者?超鏈接?或者?圖片??
????BgnCharToNoneRex?=?re.compile(“(\t|\n|?||)“)??
??????
????#?用非?貪婪模式?匹配?任意<>標(biāo)簽??
????EndCharToNoneRex?=?re.compile(“<.*?>“)??
??
????#?用非?貪婪模式?匹配?任意標(biāo)簽??
????BgnPartRex?=?re.compile(“
“)??
????CharToNewLineRex?=?re.compile(“(
||||)“)??
????CharToNextTabRex?=?re.compile(““)??
??
????#?將一些html的符號實(shí)體轉(zhuǎn)變?yōu)樵挤??
????replaceTab?=?[(“<““<“)(“>““>“)(“&““&“)(“&““\““)(“?““?“)]??
??????
????def?Replace_Char(selfx):??
????????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?xw_News:??
????#?申明相關(guān)的屬性??
????def?__init__(selfurl):????
????????self.myUrl?=?url??
????????self.datas?=?[]??
????????self.myTool?=?HTML_Tool()??
????????print?u‘已經(jīng)啟動360新聞爬蟲,咔嚓咔嚓‘??
????
????#?初始化加載頁面并將其轉(zhuǎn)碼儲存??
????def?News(self):??
????????#?讀取頁面的原始信息并將其從gbk轉(zhuǎn)碼??
????????myPage?=?urllib2.urlopen(self.myUrl).read().decode(“utf-8“)???
????????#?獲取最終的數(shù)據(jù)??
????????self.save_data(self.myUrl)???
????#?用來存儲樓主發(fā)布的內(nèi)容??
????def?save_data(selfurl):??
????????#?加載頁面數(shù)據(jù)到數(shù)組中??
????????self.get_data(url)??
????????#?打開本地文件??
????????f?=?open((‘今日新聞頭條.doc‘).decode(‘utf-8‘)‘w+‘)??
????????f.writelines(self.datas)??
????????f.close()??
????????print?u‘爬蟲報告:文件已下載到本地并打包成doc文件‘??
????????print?u‘請按任意鍵退出...‘??
????????raw_input();??
??
????#?獲取頁面源碼并將其存儲到數(shù)組中??
????def?get_data(selfurl):??
????????????myPage?=?urllib2.urlopen(url).read()??
????????????#?將myPage中的html代碼處理并存儲到datas里面??
????????????self.deal_data(myPage.decode(‘utf-8‘))??
??????????????
??
????#?將內(nèi)容從頁面代碼中摳出來??
????def?deal_data(selfmyPage):??
????????myItems?=?re.findall(‘(.*?)‘myPagere.S)
????????for?item?in?myItems:
????????????data?=?self.myTool.Replace_Char(item[1].replace(“\n“““).encode(‘utf-8‘))
????????????self.datas.append(data+‘\n‘)
????????????data?=?self.myTool.Replace_Char(item[0].replace(“\n“““).encode(‘utf-8‘))
????????????self.datas.append(data+‘\n‘)
??????????????
??
??
??
#--------?程序入口處?------------------??
print?u“““#---------------------------------------?
#???程序:360新聞標(biāo)題獲取?
#???版本:0.5?
#???作者:閑云?
#???日期:2013-11-03?
#???語言:Python?2.7?
#???操作:獲取360當(dāng)日的新聞標(biāo)題?
#???功能:將360新聞的內(nèi)容打包doc存儲到本地。?
#---------------------------------------?
“““??
??
??
print?u‘360新聞獲取‘??
bdurl?=
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3760??2013-11-03?19:11??python爬蟲樣例\360新聞爬蟲.py
?????文件???????5531??2013-11-02?22:53??python爬蟲樣例\百度貼吧看小說爬蟲.py
?????文件???????4932??2013-11-02?22:21??python爬蟲樣例\糗事百科爬蟲.py
?????目錄??????????0??2013-11-16?12:20??python爬蟲樣例
-----------?---------??----------?-----??----
????????????????14223????????????????????4
評論
共有 條評論
相關(guān)資源
-
二級考試python試題12套(包括選擇題和
-
pywin32_python3.6_64位
-
python+ selenium教程
-
PycURL(Windows7/Win32)Python2.7安裝包 P
-
英文原版-Scientific Computing with Python
-
7.圖像風(fēng)格遷移 基于深度學(xué)習(xí) pyt
-
基于Python的學(xué)生管理系統(tǒng)
-
A Byte of Python(簡明Python教程)(第
-
Python實(shí)例174946
-
Python 人臉識別
-
Python 人事管理系統(tǒng)
-
一個多線程智能爬蟲,爬取網(wǎng)站小說
-
基于python-flask的個人博客系統(tǒng)
-
計算機(jī)視覺應(yīng)用開發(fā)流程
-
python 調(diào)用sftp斷點(diǎn)續(xù)傳文件
-
python socket游戲
-
基于Python爬蟲爬取天氣預(yù)報信息
-
python函數(shù)編程和講解
-
頂點(diǎn)小說單本書爬蟲.py
-
Python開發(fā)的個人博客
-
基于python的三層神經(jīng)網(wǎng)絡(luò)模型搭建
-
python實(shí)現(xiàn)自動操作windows應(yīng)用
-
python人臉識別(opencv)
-
python 繪圖(方形、線條、圓形)
-
python疫情卡UN管控
-
python 連連看小游戲源碼
-
基于PyQt5的視頻播放器設(shè)計
-
一個簡單的python爬蟲
-
csv文件行列轉(zhuǎn)換python實(shí)現(xiàn)代碼
-
Python操作Mysql教程手冊