資源簡介
分享源碼
代碼片段和文件信息
‘‘‘
Created?on?2016年3月31日
@author:?yawenchen
‘‘‘
#?-*-?coding:utf-8?-*-
from?urllib.error?import?HTTPErrorURLError
import?urllib.request
import?re
import?urllib.parse
#處理頁面標(biāo)簽類
class?Tool:
????#去除img標(biāo)簽,7位長空格??,{m}匹配前一個字符m次
????removeImg?=?re.compile(‘|?{7}|‘)???
????#刪除超鏈接標(biāo)簽
????removeAddr?=?re.compile(‘|‘)
????
????#把換行的標(biāo)簽換成\n
????replaceLine?=?re.compile(‘|||‘)
????
????#將表格制表替換為\t
????replaceTD?=?re.compile(‘ ‘)
????
????#把段落開頭換為\n加空兩格
????replacePara?=?re.compile(‘‘)
????
????#將換行符或雙換行符替換為\n
????replaceBR?=?re.compile(‘
|
‘)
????
????#將其余標(biāo)簽剔除
????removeExtraTag?=?re.compile(‘<.*?>‘)
????
????def?replace(selfx):
????????x?=?re.sub(self.removeImg““x)
????????x?=?re.sub(self.removeAddr““x)
????????x?=?re.sub(self.replaceLine“\n“x)
????????x?=?re.sub(self.replaceTD“\t“x)
????????x?=?re.sub(self.replacePara“\n“x)
????????x?=?re.sub(self.replaceBR“\n“x)
????????x?=?re.sub(self.removeExtraTag““x)
????????
????????#strip()將前后多余內(nèi)容刪除
????????return?x.strip()
????
#百度貼吧爬蟲類
class?BDTB:
????#初始化,傳入基地址,是否只看樓主的參數(shù)
????def?__init__(selfbaseUrlseeLZfloorTag):
????????#base鏈接地址
????????self.baseURL?=?baseUrl
????????#是否只看樓主
????????self.seeLZ?=?‘?see_lz‘+str(seeLZ)
????????#html標(biāo)簽剔除工具類對象
????????self.tool?=?Tool()
????????#全局file變量,文件寫入操作對象
????????self.file?=?None
????????#樓層標(biāo)號,初始為1
????????self.floor?=?1
????????#默認(rèn)的標(biāo)題,如果沒有成功獲取到標(biāo)題的話,則會用這個標(biāo)題
????????self.defaulttitle?=?u“百度貼吧“
????????#是否寫入樓層分割符的標(biāo)記
????????self.floorTag?=?floorTag
????
????#傳入頁碼,獲取該頁帖子的代碼
????def?getPage(selfpageNum):
????????try:
????????????#構(gòu)建URL
????????????url?=?self.baseURL?+?self.seeLZ?+?‘&pn=‘?+?str(pageNum)
????????????request?=?urllib.request.Request(url)
????????????response?=?urllib.request.urlopen(request)
????????????#返回utf-8格式編碼內(nèi)容
????????????return?response.read().decode(‘utf-8‘)
????????except?URLError?as?e:
????????????if?hasattr(e“reason“):
????????????????print(“連接百度貼吧失敗,錯誤原因:“e.reason)
????????????????return?None
????#獲取帖子標(biāo)題
????def?gettitle(selfpage):
????????#得到標(biāo)題的正則表達(dá)式
????????pattern?=?re.compile(‘tle_txt.*?>(.*?)
‘re.S)
????????result?=?re.search(patternpage)
????????if?result:
#?????????????print(“標(biāo)題:“result.group(1))??#測試輸出
????????????#如果標(biāo)題存在,則返回標(biāo)題
????????????return?result.group(1).strip()
????????else:
????????????return?None
????#獲取帖子一共有多少頁
????def?getPageNum(self
評論
共有 條評論
相關(guān)資源
-
pywin32_python3.6_64位
-
python3環(huán)境搭建教程.ppt
-
python3.5可用的scipy
-
PYTHON3 經(jīng)典50案例.pptx
-
python3.5.2.chm官方文檔
-
python3.5.4_windows下32與64位安裝包
-
GitHack-python3.zip
-
Python3.x+PyQtChart實(shí)現(xiàn)數(shù)據(jù)可視化界面
-
python3根據(jù)模板圖片批量自動制作個性
-
《機(jī)器學(xué)習(xí)實(shí)戰(zhàn)》源代碼Python3
-
Python3.5.2的IDLE漢化版計(jì)算機(jī)等級考試
-
python3.5 百度ai人臉識別
-
python3實(shí)現(xiàn)的國密SM2+SM3
-
python100道面試題及解答全部答案 pyc
-
#python3.3關(guān)于Tk中的Treeview使用方法
-
Python3.x+Pyqt5實(shí)現(xiàn)界面編程瀏覽網(wǎng)頁
-
《機(jī)器學(xué)習(xí)實(shí)戰(zhàn)》Python3代碼
-
Python3學(xué)習(xí)筆記
-
Python3.7.2中文文檔-標(biāo)準(zhǔn)庫-通用操作系
-
Python3.7.2中文文檔-標(biāo)準(zhǔn)庫-Python數(shù)據(jù)類
-
python3基礎(chǔ)教程第三版高清
-
隨機(jī)森林python3實(shí)現(xiàn)代碼(帶數(shù)據(jù)集)
-
dlib-19.18.0-cp37-cp37m-linux_armv7l.whl
-
python3學(xué)習(xí)路線+思維導(dǎo)圖
-
Learn python3 the hard way
-
笨辦法學(xué)python 3 第四版 英文
-
python3爬蟲
-
Beginning Python: From Novice to Professional
-
Python3實(shí)現(xiàn)KNN的三個包含數(shù)據(jù)集,水果
-
python3中文幫助手冊和英文幫助手冊