-
Notifications
You must be signed in to change notification settings - Fork 0
/
spider_main.py
75 lines (59 loc) · 2.01 KB
/
spider_main.py
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
#coding:utf-8
'''
Title: I am not Drug God
Author:Haijun Ma
Time:2018/11/19
'''
import url_manager,html_parser,html_outputer,html_downloader,word_cloud
class SpiderMain(object):
def __init__(self):
# URL管理器
self.urls = url_manager.UrlManager()
# 网页下载器
self.downloader = html_downloader.HtmlDownloader()
# 数据提取器
self.parser = html_parser.HtmlParser()
# 数据处理器
self.outputer = html_outputer.HtmlOutputer()
# 云图生成器
self.cloud = word_cloud.Wordcloud()
def craw(self, root_url):
count =1
# 爬虫入口URL
self.urls.add_new_url(root_url)
# 待爬取URL
wait_url = self.urls.has_new_url()
if wait_url is not None:
while wait_url:
try:
# 获取一个待爬取URL
new_url = self.urls.get_new_url()
print("carw %d : %s" % (count, new_url))
# 爬取页面
html_cont = self.downloader.download(new_url)
# 数据提取
new_url, new_datas = self.parser.parser(new_url, html_cont)
# 添加新待爬取URL
self.urls.add_new_url(new_url)
# 数据加工处理
self.outputer.collect_data(new_datas)
# 爬虫循环控制
if count == 10:
break
count = count + 1
except:
print("craw failed")
# 数据加工输出
self.outputer.process_data()
#print("finish")
# 分词
self.outputer.cut_str()
# 生成云图
self.cloud.make()
print("finish")
if __name__ == "__main__":
# 爬虫入口URL
root_url = "https://movie.douban.com/subject/26752088/comments?start=10&limit=20"
obj_spider = SpiderMain()
# 启动爬虫
obj_spider.craw(root_url)