fjl-vxee

百度贴吧的爬虫制作和糗百的爬虫制作原理基本相同,都是通过查看源码扣出关键数据,然后将其存储到本地txt文件。

源码下载:

http://download.csdn.net/detail/wxg694175346/6925583

项目内容:

用Python写的百度贴吧的网络爬虫。

使用方法:

新建一个BugBaidu.py文件,然后将代码复制到里面后,双击运行。

程序功能:

将贴吧中楼主发布的内容打包txt存储到本地。

原理解释:

首先,先浏览一下某一条贴吧,点击只看楼主并点击第二页之后url发生了一点变化,变成了:

http://tieba.baidu.com/p/3684225591?see_lz=1

可以看出来,see_lz=1是只看楼主,pn=1是对应的页码,记住这一点为以后的编写做准备。

这就是我们需要利用的url。

接下来就是查看页面源码。

首先把题目抠出来存储文件的时候会用到。

 

百度贴吧的标题:<title>《凡人修仙传》 之中 一些 “路人”的故事_凡人修仙传吧_百度贴吧</title>

 

源码:

  1 # -*- coding: utf-8 -*-
  2 #---------------------------------------
  3 #   程序:百度贴吧爬虫
  4 #   版本:0.5
  5 #   作者:fjl
  6 #   日期:2016-2-11
  7 #   语言:Python 2.7
  8 #   操作:输入网址后自动只看楼主并保存到本地文件
  9 #   功能:将楼主发布的内容打包txt存储到本地。
 10 #---------------------------------------
 11  
 12 import string
 13 import urllib2
 14 import re
 15 
 16 #----------- 处理页面上的各种标签 -----------
 17 class HTML_Tool:
 18     # 用非 贪婪模式 匹配 \t 或者 \n 或者 空格 或者 超链接 或者 图片
 19     BgnCharToNoneRex = re.compile("(\t|\n| |<a.*?>|<img.*?>)")
 20     
 21     # 用非 贪婪模式 匹配 任意<>标签
 22     EndCharToNoneRex = re.compile("<.*?>")
 23 
 24     # 用非 贪婪模式 匹配 任意<p>标签
 25     BgnPartRex = re.compile("<p.*?>")
 26     CharToNewLineRex = re.compile("(<br/>|</p>|<tr>|<div>|</div>)")
 27     CharToNextTabRex = re.compile("<td>")
 28 
 29     # 将一些html的符号实体转变为原始符号
 30     replaceTab = [("<","<"),(">",">"),("&","&"),("&","\""),(" "," ")]
 31     
 32     def Replace_Char(self,x):
 33         x = self.BgnCharToNoneRex.sub("",x)
 34         x = self.BgnPartRex.sub("\n    ",x)
 35         x = self.CharToNewLineRex.sub("\n",x)
 36         x = self.CharToNextTabRex.sub("\t",x)
 37         x = self.EndCharToNoneRex.sub("",x)
 38 
 39         for t in self.replaceTab:  
 40             x = x.replace(t[0],t[1])  
 41         return x  
 42     
 43 class Baidu_Spider:
 44     # 申明相关的属性
 45     def __init__(self,url):  
 46         self.myUrl = url + \'?see_lz=1\'
 47         self.datas = []
 48         self.myTool = HTML_Tool()
 49         print u\'已经启动百度贴吧爬虫,咔嚓咔嚓\'
 50   
 51     # 初始化加载页面并将其转码储存
 52     def baidu_tieba(self):
 53         # 读取页面的原始信息并将其从gbk转码
 54         myPage = urllib2.urlopen(self.myUrl).read().decode("utf-8")
 55         # 计算楼主发布内容一共有多少页
 56         endPage = self.page_counter(myPage)
 57         # 获取该帖的标题
 58         title = self.find_title(myPage)
 59         print u\'文章名称:\' + title
 60         # 获取最终的数据
 61         self.save_data(self.myUrl,title,endPage)
 62 
 63     #用来计算一共有多少页
 64     def page_counter(self,myPage):
 65         # 匹配 "共有<span class="red">12</span>页" 来获取一共有多少页
 66         myMatch = re.search(r\'class="red">(\d+?)</span>\', myPage, re.S)
 67         if myMatch:  
 68             endPage = int(myMatch.group(1))
 69             print u\'爬虫报告:发现楼主共有%d页的原创内容\' % endPage
 70         else:
 71             endPage = 0
 72             print u\'爬虫报告:无法计算楼主发布内容有多少页!\'
 73         return endPage
 74 
 75     # 用来寻找该帖的标题
 76     def find_title(self,myPage):
 77         # 匹配 <h1 class="core_title_txt" title="">xxxxxxxxxx</h1> 找出标题
 78         myMatch = re.search(r\'<title>(.*?)</title>\', myPage, re.S)
 79         title = u\'暂无标题\'
 80         if myMatch:
 81             #title  = myMatch.group(1)
 82             title  = myMatch.group(1)
 83         else:
 84             print u\'爬虫报告:无法加载文章标题!\'
 85         # 文件名不能包含以下字符: \ / : * ? " < > |
 86         title = title.replace(\'\\\',\'\').replace(\'/\',\'\').replace(\':\',\'\').replace(\'*\',\'\').replace(\'?\',\'\').replace(\'"\',\'\').replace(\'>\',\'\').replace(\'<\',\'\').replace(\'|\',\'\')
 87         return title
 88 
 89 
 90     # 用来存储楼主发布的内容
 91     def save_data(self,url,title,endPage):
 92         # 加载页面数据到数组中
 93         self.get_data(url,endPage)
 94         # 打开本地文件
 95         f = open(title+\'.txt\',\'w+\')
 96         f.writelines(self.datas)
 97         f.close()
 98         print u\'爬虫报告:文件已下载到本地并打包成txt文件\'
 99         print u\'请按任意键退出...\'
100         raw_input();
101 
102     # 获取页面源码并将其存储到数组中
103     def get_data(self,url,endPage):
104         url = url + \'&pn=\'
105         for i in range(1,endPage+1):
106             print u\'爬虫报告:爬虫%d号正在加载中...\' % i
107             myPage = urllib2.urlopen(url + str(i)).read()
108             # 将myPage中的html代码处理并存储到datas里面
109             self.deal_data(myPage.decode(\'utf-8\'))
110             
111 
112     # 将内容从页面代码中抠出来
113     def deal_data(self,myPage):
114         myItems = re.findall(\'id="post_content.*?>(.*?)</div>\',myPage,re.S)
115         for item in myItems:
116             data = self.myTool.Replace_Char(item.replace("\n","").encode(\'utf-8\'))
117             self.datas.append(data+\'\n\')
118 
119 
120 
121 #-------- 程序入口处 ------------------
122 print u"""#---------------------------------------
123 #   程序:百度贴吧爬虫
124 #   版本:0.5
125 #   作者:fjl
126 #   日期:2016-2-11
127 #   语言:Python 2.7
128 #   操作:输入网址后自动只看楼主并保存到本地文件
129 #   功能:将楼主发布的内容打包txt存储到本地。
130 #---------------------------------------
131 """
132 
133 # 以某小说贴吧为例子
134 # bdurl = \'http://tieba.baidu.com/p/3684225591?see_lz=1\'
135 
136 print u\'请输入贴吧的地址最后的数字串:\'
137 bdurl = \'http://tieba.baidu.com/p/\' + str(raw_input(u\'http://tieba.baidu.com/p/\')) 
138 
139 #调用
140 mySpider = Baidu_Spider(bdurl)
141 mySpider.baidu_tieba()

参考:http://blog.csdn.net/pleasecallmewhy/article/details/8934726

分类:

技术点:

相关文章: