为了账号安全,请及时绑定邮箱和手机立即绑定

AttributeError: 'UrlManager' object has no attribute 'new_urls'

Traceback (most recent call last):

  File "H:\python\qthnluwei\src\baike\spider_main.py", line 33, in <module>

    obj_Spider.craw(root_url)

  File "H:\python\qthnluwei\src\baike\spider_main.py", line 13, in craw

    self.urls.add_new_url (root_url)

  File "H:\python\qthnluwei\src\baike\url_manager.py", line 11, in add_new_url

    if url not in self .new_urls and url not in self.old_urls:

AttributeError: 'UrlManager' object has no attribute 'new_urls'

根据老师讲的运行了程序,但报错。没有对new_urls定义?请问如何进行处理。

正在回答

2 回答

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:
            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



0 回复 有任何疑惑可以回复我~
#1

慕粉3851000 提问者

非常感谢!
2017-02-23 回复 有任何疑惑可以回复我~

没看懂 这段代码有变化吗?

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Python开发简单爬虫
  • 参与学习       227670    人
  • 解答问题       1219    个

本教程带您解开python爬虫这门神奇技术的面纱

进入课程

AttributeError: 'UrlManager' object has no attribute 'new_urls'

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信