利用Python写百度贴吧爬虫
最近,我们这边需要做一次防爬虫和机器蜘蛛的困扰,感觉困惑,有点无从入手,倒不如,直接用Python来写一个Spiner理解其各种原理,再下手也不迟啊,于是便立刻去写一个爬虫程序。使用方法:
新建一个BugBaidu.py文件,然后将代码复制到里面后,双击运行。
程序功能:
将贴吧中楼主发布的内容打包txt存储到本地。
好,不废话,直接上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python
#-*- coding: utf-8 -*-
import string
import urllib2
import re
# ----------- 处理页面上的各种标签 -----------
class HTML_Tool:
# 用非 贪婪模式 匹配 \t 或者 \n 或者 空格 或者 超链接 或者 图片
BgnCharToNoneRex = re.compile("(\t|\n| |<a.*?>|<img.*?>)")
# 用非 贪婪模式 匹配 任意<>标签
EndCharToNoneRex = re.compile("<.*?>")
# 用非 贪婪模式 匹配 任意<p>标签
BgnPartRex = re.compile("<p.*?>")
CharToNewLineRex = re.compile("(<br/>|</p>|<tr>|<div>|</div>)")
CharToNextTabRex = re.compile("<td>")
# 将一些html的符号实体转变为原始符号
replaceTab = [("<", "<"), (">", ">"), ("&", "&"), ("&", "\""), (" ", " ")]
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)
for t in self.replaceTab:
x = x.replace(t, t)
return x
class Yzw_Spider:
# 申明相关的属性
def __init__(self, url):
self.myUrl = url + '?see_lz=1'
self.datas = []
self.myTool = HTML_Tool()
print u'已经启动爬虫,咔嚓咔嚓'
# 初始化加载页面并将其转码储存
def yzw_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)
# 用来计算一共有多少页
def page_counter(self, myPage):
# 匹配 "共有<span class="red">12</span>页" 来获取一共有多少页
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):
# 匹配 <h1 class="core_title_txt" title="">xxxxxxxxxx</h1> 找出标题
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(
'>', '').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()
# 将myPage中的html代码处理并存储到datas里面
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('gbk'))
self.datas.append(data + '\n')
# -------- 程序入口处 ------------------
print u"""#---------------------------------------
# 程序:一站网爬虫
# 语言:Python 2.7
# 操作:输入网址后自动只看楼主并保存到本地文件
# 功能:将楼主发布的内容打包txt存储到本地。
#---------------------------------------
"""
# 以某热点推荐为例子
# bdurl = 'http://tieba.baidu.com/p/2296712428?see_lz=1&pn=1'
print u'请输入热点的地址最后的数字串:'
bdurl = 'http://tieba.baidu.com/p/' + str(raw_input(u'http://tieba.baidu.com/p/'))
# 调用
mySpider = Yzw_Spider(bdurl)
mySpider.yzw_tieba()
最后,得出的是一个.txt的文档,但里面有我需要爬下来的内容,如图:
就这样一个爬虫程序就OK了
页:
[1]