-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtSpiderV2.py
More file actions
409 lines (352 loc) · 10.2 KB
/
tSpiderV2.py
File metadata and controls
409 lines (352 loc) · 10.2 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# coding: utf-8
"""
直接执行本文本,会在当前目前目录创建文件夹spider_res来保存结果
"""
from bs4 import BeautifulSoup # 用于解析网页中文, 安装: pip install beautifulsoup4
import os
import re
import time
import urllib2
import urlparse
def download(url, retry=2):
"""
下载页面的函数,会下载完整的页面信息
:param url:
:param retry:
:return:
"""
print "downloading: ", url
# 设置header信息,模拟浏览器请求
header = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36'
}
try:
request = urllib2.Request(url, headers=header)
html = urllib2.urlopen(request, timeout=2).read()
except urllib2.URLError as e:
print "download error: ", e.reason
html = None
if retry > 0:
if hasattr(e, 'code') and 500 <= e.code < 600:
print e.code
return download(url, retry - 1)
time.sleep(1)
return html
def getDOM(soap, domPath):
'''
筛选元素
:param soap: soap化html页面
:param domPath: 要筛选的元素dom路径
:return:
:return:
'''
res = soap
for tag in domPath:
for k in tag:
res = res.find(k, tag[k])
return res
def getRows(str):
'''
从soap之后text结果中获取行
:param str:
:return:
'''
res = []
pattern = re.compile(r'( |\xa0)+') # 替换空格
str = pattern.subn(' ', str)
pattern = re.compile(r"\n")
rows = pattern.split(str[0])
for row in rows:
row = tripStr(row)
if row != '':
res.append(row)
return res
def tripStr(str):
'''
去除行两端空格z
:param str:
:return:
'''
return str.strip()
def getHTags(soap, rows):
'''
获取所有h级别标签(h1->h6)
:return:
'''
tags = [
{'h1': '# '},
{'h2': '## '},
{'h3': '### '},
{'h4': '#### '},
{'h5': '##### '},
{'h6': '###### '}
]
repRows = []
for tag in tags: # 先找到tag,然后再修改rows
for k in tag:
items = soap.find_all(k)
for item in items:
txt = tripStr(item.text)
if txt != '':
rep = "\n\n" + tag[k] + txt + "\n\n"
repRows.append({txt: rep})
newRows = []
for row in rows:
row, repRows = replaceRow(row, repRows)
newRows.append(row)
return newRows
def replaceRow(row, repRows):
'''
替换行内容,匹配晚餐之后,从匹配堆中删除已经匹配的结果
:param row: 要替换的行
:param repRows: 要替换的内容列表
:return:
'''
newRepRows = []
for rep in repRows:
for k in rep:
if k != '':
if row.find(k) != -1:
row = row.replace(k, rep[k])
else:
newRepRows.append({k: rep[k]}) # 未被匹配的数据重新记入待匹配数据集中
else:
newRepRows.append({k: rep[k]})
return row, newRepRows
def getPTags(soap):
'''
处理p标签
:param soap:
:return:
'''
repRows = []
items = soap.find_all('p')
for item in items:
txt = tripStr(item.text)
if txt != '':
rep = tripStr(item.text) + "\n\n"
repRows.append({txt: rep})
return repRows
def getBoldTags(soap):
'''
获取所有粗体标签内容
:param soap:
:return:
'''
tags = [
{'strong': '__%s__'},
{'b': '__%s__'}
]
repRows = []
for tag in tags:
for k in tag:
items = soap.find_all(k)
for item in items:
txt = tripStr(item.text)
if txt != '':
rep = tag[k] % (txt)
repRows.append({txt: rep})
return repRows
def getATags(soap):
'''
获取a标签
:param soap:
:return:
'''
repRows = []
items = soap.find_all('a')
for item in items:
txt = tripStr(item.text)
if txt != '':
url = item.get('href')
rep = '%s[%s](%s)' % (txt, url, url)
repRows.append({txt: rep})
return repRows
def getCodeTags(soap):
'''
获取代码标签
:param soap:
:return:
'''
repRows = []
items = soap.find_all('pre')
for item in items:
rows = getRows(item.text)
newRows = []
for txt in rows:
if txt != '':
newRows.append({txt: txt})
if (newRows.__len__() > 0):
for k in newRows[0]:
newRows[0] = {k: "\n```\n%s" % newRows[0][k]}
for k in newRows[-1]:
newRows[-1] = {k: "%s\n```\n" % newRows[-1][k]}
repRows = repRows + newRows
return repRows
def getImgTags(soap, save=False):
'''
获取图片
:param soap:
:param save: 想保存图片到本地,true
:return:
'''
repRows = []
items = soap.find_all('div', {'class': 'image-package'})
for item in items:
imgUrl = item.find('img').get('src')
imgUrl = imgUrl[: imgUrl.find('?')]
if save == True: # 下载图片到本地
link = imgUrl
path = link.split('/')
name = path[-1]
savePath = 'spider_res/' + name
if os.path.exists(savePath) == False:
source = download(link)
if source != None:
file = open(savePath, 'wb')
file.write(source)
file.close()
txt = tripStr(item.find('div', {'class': 'image-caption'}).text) # 可能为空
if txt == '': # 无文本信息,url作为文本信息
txt = imgUrl
rep = "\n%s" % (imgUrl, imgUrl, txt)
repRows.append({txt: rep})
return repRows
def getBQTags(soap):
'''
blockquote标签处理
:param soap:
:return:
'''
repRows = []
items = soap.find_all('blockquote')
for item in items:
block = getRows(item.text)
num = block.__len__()
cur = 0
for b in block:
cur += 1
b = tripStr(b)
if b != '':
rep = '> %s' % (b)
if (cur == num):
rep = rep + "\n\n--\n"
repRows.append({b: rep})
return repRows
def getTitle(soap):
'''
获取文章标题
:param soap:
:return:
'''
# 文章标题 div.article->h1.title
titlePath = [
{'div': {'class': 'article'}},
{'h1': {'class': 'title'}}
]
# markdown一级标题
title = tripStr(getDOM(soap, titlePath).string)
return title
def getAuthor(soap):
'''
获取作者名字
:param soap:
:return:
'''
authorPath = [
{'div': {'class': 'article'}},
{'div': {'class': 'author'}},
{'div': {'class': 'info'}}
]
authorAll = getDOM(soap, authorPath)
author = authorAll.find('span', {'class': 'tag'}).string + ': ' + authorAll.find('span', {'class': 'name'}).string
return author
def crawlerOnePage(link):
'''
抓取单个网页
:param link:
:return:
'''
html = download(link)
if html == None:
return
soap = BeautifulSoup(html, "html.parser")
title = getTitle(soap)
# file_name = 'spider_res/%s.md' % title
# if os.path.exists(file_name): # 文件存在,不处理
# return
content = soap.find('div', {'class': 'show-content'})
rows = getRows(content.text)
pTags = getPTags(content) # p标签需要最先处理
newRows = []
for row in rows:
for p in pTags:
for k in p:
if k == '':
continue
if row.find(k) != -1:
row = row.replace(k, p[k])
newRows.append(row)
rows = newRows
rows = getHTags(content, rows) # h标签需要单独处理
reps = getBQTags(content)
reps = reps + getCodeTags(content)
reps = reps + getBoldTags(content)
reps = reps + getATags(content)
reps = reps + getImgTags(content)
newRows = []
for row in rows:
row, reps = replaceRow(row, reps)
newRows.append(row)
if reps.__len__() > 0: # 还有部分数据未被匹配,添加到最后
for rep in reps:
for k in rep:
newRows.append(rep[k])
author = getAuthor(soap)
writeFile(title, author, link, newRows)
def writeFile(title, author, url, rows):
if os.path.exists('spider_res/') == False:
os.mkdir('spider_res')
file_name = 'spider_res/' + title + '.md'
if os.path.exists(file_name) == False:
file = open(file_name, 'wb')
title = '# ' + unicode(title).encode('utf-8', errors='ignore') + "\n\n"
file.write(title)
url = '[%s](%s)' % (url, url)
url = unicode(url).encode('utf-8', errors='ignore') + "\n\n"
file.write(url)
author = unicode(author).encode('utf-8', errors='ignore') + "\n\n"
file.write(author)
for row in rows:
row = unicode(tripStr(row)).encode('utf-8', errors='ignore') + "\n\n"
file.write(row)
file.close()
url_root = 'http://www.jianshu.com/' # 网站根目录
url_seed = 'http://www.jianshu.com/c/9b4685b6357c?page=%d' # 要爬取的页面地址模板
crawled_url = set() # 已经爬取过的链接
flag = True
# step1 抓取文章链接
i = 1
while flag:
url = url_seed % i
i += 1
html = download(url)
if html == None:
break
soap = BeautifulSoup(html, "html.parser")
links = soap.find_all('a', {'class': 'title'})
if links.__len__() == 0:
flag = False
for link in links:
link = link.get('href')
if link not in crawled_url:
realUrl = urlparse.urljoin(url_root, link)
crawled_url.add(realUrl) # 已爬取的页面记录下来,避免重复爬取
else:
print 'end'
flag = False # 结束抓取
paper_num = crawled_url.__len__()
print 'total paper num: ', paper_num
# step2 抓取文章内容,并按标题和内容保存起来
for link in crawled_url:
crawlerOnePage(link)