module 'url_manager' has no attribute 'UrlManager'
#这是我的主代码,
#coding:utf-8
import url_manager,html_downloader,html_parser,html_outputer
class SpiderMain(object):
def __init__(self):
# 在构造函数中进行初始化各个对象
self.urls=url_manager.UrlManager()#url的管理器
self.downloader=html_downloader.HtmlDownloader()#下载器
self.parser=html_parser.HtmlParser()#解析器
self.outputer=html_outputer.HtmlOutputer()#输出器
# 爬虫的调度程序
def craw(self,root_url):
count = 1
self.urls.add_new_url(root_url)
while self.urls.has_new_url():
try:
new_url = self.urls.get_new_url()
print('craw %d:%s'%(count,new_url))
html_cont = self.downloader.download(new_url)
new_urls,new_data=self.parser.parse(new_url,html_cont)
self.urls.add_new_urls(new_urls)
self.outputer.collect_data(new_data)
if count==1000:
break
count = count+1
except:
print('craw failed')
self.outputer.output_html()
if __name__=="__main__":
root_url = "https://baike.baidu.com/item/python/407313"
obj_spider = SpiderMain()
obj_spider.craw(root_url)
class UrlManager(object):
"""docstring for UrlManager"""
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:
self.new_urls.add(url)
def add_new_urls(self, urls):
if urls is None or len(urls)==0:
return
for url in urls:
self.add_new_url(url)
def has_new_url(self):
return len(self.new_urls) != 0
def get_new_url(self):
new_url = self.new_urls.pop()
self.old_urls.add(new_url)
return new_url
这是url_manager.py的代码,为什么先显示没有UrlManager的属性呢?很迷惑,找了一天的bug了都没找到,