forked from SiChengXin/imoocWebCrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathURLManager.py
35 lines (29 loc) · 975 Bytes
/
URLManager.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
# -*- coding: utf-8 -*-
class UrlManager(object):
def __init__(self):
self.new_urls = set()
self.old_urls = set()
def add_new_url(self, url):
if url is None:
return
if url not in self.new_urls and url not in self.old_urls:
# 如果该URL没有被/添加访问过就添加进管理器
self.new_urls.add(url)
pass
def add_new_urls(self, urls):
if urls is None or len(urls) == 0:
return
for url in urls:
# 将URL列表添加进待访问队列
self.new_urls.add(url)
pass
def has_new_url(self):
# 返回URL池是否为空
return len(self.new_urls) != 0
pass
def get_new_url(self):
# 从未访问的URL中取出一个,并返回取出的URL
new_url = self.new_urls.pop()
self.old_urls.add(new_url)
return new_url
pass