forked from F-SLoong/douban-spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spider_main.py
36 lines (30 loc) · 1.47 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
import url_manager,html_downloader,html_parser,html_outputer
import time
class SpiderMain(object):
def __init__(self):
self.urls = url_manager.UrlManager() #url管理器
self.downloader = html_downloader.HtmlDownloader() #html网页下载器
self.parser = html_parser.HtmlParser() #html分析器
self.outputer = html_outputer.HtmlOutputer() #html输出器
def craw(self, root_url):
count = 1
self.urls.add_new_url(root_url)
try:
while self.urls.has_new_url():
new_url = self.urls.get_new_url() #从url管理器中获取一个未爬取的url
print ('craw %d : %s' % (count, new_url))
html_cont = self.downloader.download(new_url) #下载该url的html
new_urls, new_data = self.parser.parse(new_url, html_cont) #分析html,返回urls和data
self.urls.add_new_urls(new_urls) #将获取的urls添加进未爬取的url集合中,排除已爬取过的url
self.outputer.collect_data(new_data) #数据都在内存中
time.sleep(0.1)
if count == 10:
break
count += 1
except:
print ('craw failed')
self.outputer.output_html()
if __name__ == "__main__":
root_url = "https://book.douban.com/subject/1477390/" #起始地址为《代码大全》
obj_spider = SpiderMain()
obj_spider.craw(root_url)