python爬虫百度贴吧代码大全_python爬虫实战(2)--爬取百度贴吧

本文介绍了一个Python 2.7编写的百度贴吧爬虫程序,该程序可以自动只看楼主并保存内容到本地TXT文件。通过输入帖子URL,爬虫能抓取并格式化处理帖子的全部楼层内容。
摘要由CSDN通过智能技术生成

#-*- coding: utf-8 -*-#---------------------------------------#程序:百度贴吧爬虫#作者:lzhc#日期:2017-12-11#语言:Python 2.7#操作:输入网址后自动只看楼主并保存到本地文件#功能:将楼主发布的内容打包txt存储到本地。#---------------------------------------

importstringimporturllib2importre#----------- 处理页面上的各种标签 -----------

classHTML_Tool:#用非 贪婪模式 匹配 \t 或者 \n 或者 空格 或者 超链接 或者 图片

BgnCharToNoneRex = re.compile("(\t|\n| ||)")#用非 贪婪模式 匹配 任意<>标签

EndCharToNoneRex = re.compile("<.>")#用非 贪婪模式 匹配 任意

标签

BgnPartRex = re.compile("

")

CharToNewLineRex= re.compile("(
|

||
|
)")

CharToNextTabRex= re.compile("

")defReplace_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)returnxclassBaidu_Spider:#申明相关的属性

def __init__(self, url):

self.myUrl= url + '?see_lz=1'self.datas=[]

self.myTool=HTML_Tool()

self.floor= 1

print u'已经启动百度贴吧爬虫'

#初始化加载页面并将其转码储存

defbaidu_tieba(self):#读取页面的原始信息并将其从utf-8转码

myPage = urllib2.urlopen(self.myUrl).read().decode('utf-8')#计算楼主发布内容一共有多少页

endPage =self.page_counter(myPage)#获取该帖的标题

title =self.find_title(myPage)print u'文章名称:' +title#获取最终的数据

self.save_data(self.myUrl, title, endPage)#用来计算一共有多少页

defpage_counter(self, myPage):#匹配 "共有12页" 来获取一共有多少页

myMatch = re.search(r'class="red">(\d+?)', myPage, re.S)ifmyMatch:

endPage= int(myMatch.group(1))print u'爬虫报告:发现楼主共有%d页的原创内容' %endPageelse:

endPage=0print u'爬虫报告:无法计算楼主发布内容有多少页!'

returnendPage#用来寻找该帖的标题

deffind_title(self, myPage):#匹配

xxxxxxxxxx

找出标题

myMatch = re.search(r'

(.*?)', myPage, re.S)

title= u'暂无标题'

ifmyMatch:

title= myMatch.group(1)else:print u'爬虫报告:无法加载文章标题!'

#文件名不能包含以下字符: \ / : * ? " < > |

title = title.replace('\\', '').replace('/', '').replace(':', '').replace('*', '').replace('?', '').replace('"','').replace('>', '').replace('

defsave_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();#获取页面源码并将其存储到数组中

defget_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().decode('utf-8')#将myPage中的html代码处理并存储到datas里面

self.deal_data(myPage)#将内容从页面代码中抠出来

defdeal_data(self, myPage):

myItems= re.findall('id="post_content.*?>(.*?)', myPage, re.S)for item inmyItems:

data= self.myTool.Replace_Char(item.encode('gbk'))

myFloor= (str(self.floor) + "楼--------------------------------------").decode('utf-8').encode('gbk')

self.datas.append(myFloor+ '\n\n' + data + '\n\n')

self.floor+= 1

#-------- 程序入口处 ------------------

print u"""#---------------------------------------

# 程序:百度贴吧爬虫

# 作者:lzhc

# 日期:2017-12-11

# 语言:Python 2.7

# 操作:输入网址后自动只看楼主并保存到本地文件

# 功能:将楼主发布的内容打包txt存储到本地。

#---------------------------------------"""

#以中南财经政法大学迎新帖为例子#bdurl = 'http://tieba.baidu.com/p/5174106966?see_lz=1&pn=1'

print u'请输入贴吧的地址最后的数字串:'bdurl= 'http://tieba.baidu.com/p/' + str(raw_input(u'http://tieba.baidu.com/p/'))#调用

mySpider =Baidu_Spider(bdurl)

mySpider.baidu_tieba()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值