网络爬虫——爬百度贴吧
2015-12-28 21:25
549 查看
功能:输入话题的编号(一般在百度贴吧里面找)然后爬取楼主的所有发言的文字部分。
说明:中文编码和保存文件较上一篇又有新的方式,特此留存。
#coding: utf-8
import string
import urllib2
import re
class HTML_Tool:#作用就是将html文件里的一些标签去掉,只保留文字部分
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>")
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)
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):
#在做编码转换时,通常需要以unicode作为中间编码
#即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码
myPage = urllib2.urlopen(self.myUrl).read().decode("utf-8")#decode的作用是将其他编码的字符串转换成unicode编码
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("|",'')
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('utf-8'))
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('utf-8'))
self.datas.append(data+'\n')
#self.datas.append(item.replace("\n","").encode('utf-8')+'\n')
print u'请输入贴吧地址最后的字符串: '
bdurl = 'http://tieba.baidu.com/p/' + str(raw_input(u'htttp://tieba.baidu.com/p/'))
mySpider = Baidu_Spider(bdurl)
mySpider.baidu_tieba()
后记:学了一周多的爬虫,把《python学习手册》大体翻了一遍,感觉对Python语言的基础了解一些了。至于爬虫程序,参照大神的博客,依样画葫芦也算是实现了两个小爬虫。时间不允许了,我也不准备深究了,以后用到再拓展吧!
说明:中文编码和保存文件较上一篇又有新的方式,特此留存。
#coding: utf-8
import string
import urllib2
import re
class HTML_Tool:#作用就是将html文件里的一些标签去掉,只保留文字部分
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>")
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)
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):
#在做编码转换时,通常需要以unicode作为中间编码
#即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码
myPage = urllib2.urlopen(self.myUrl).read().decode("utf-8")#decode的作用是将其他编码的字符串转换成unicode编码
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("|",'')
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('utf-8'))
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('utf-8'))
self.datas.append(data+'\n')
#self.datas.append(item.replace("\n","").encode('utf-8')+'\n')
print u'请输入贴吧地址最后的字符串: '
bdurl = 'http://tieba.baidu.com/p/' + str(raw_input(u'htttp://tieba.baidu.com/p/'))
mySpider = Baidu_Spider(bdurl)
mySpider.baidu_tieba()
后记:学了一周多的爬虫,把《python学习手册》大体翻了一遍,感觉对Python语言的基础了解一些了。至于爬虫程序,参照大神的博客,依样画葫芦也算是实现了两个小爬虫。时间不允许了,我也不准备深究了,以后用到再拓展吧!
相关文章推荐
- HTTP Live Streaming直播(iOS直播)技术分析与实现
- iOS开发之http协议
- Liunx网络异常诊断及修复
- 一步一步实现ListView加载网络数据,下滑底部加载,顶部下拉刷新。并配有双缓存
- TCP粘包
- com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
- http学习01
- 15.cocos2d-x网络编程一(get、post请求)
- TCP/IP、Http的区别
- TCP聊天系统开发心得
- .NET中使用HTTP 进行POST 数据
- Python---httplib2
- Python---httplib2
- Python---http
- Python---http
- Socket通讯TCP/UDP粘包,多包和断包、少包问题解读
- 在Android Studio上使用GSON+VOLLEY,秒处理网络数据成集合。感受框架的力量。搭配RecyclerView和SwipeRefreshLayout,实现底端加载更多,下拉刷新。
- android客户端加载网络大图片如何避免内存溢出的两种方法
- HP刀片服务器系统Flex-10 VC配置与VMware vSphere网络设计
- 智能家居网络系统的设计(一)