Python Crawler - AttributeError: Crawler instance has no attribute 'url'
Question: I'm trying to learn the classes in python:

    
    
    #!/usr/bin/env python
    # *-* coding: utf-8 *-*
    
    import urllib2
    from BeautifulSoup import BeautifulSoup as bs
    
    class Crawler:
    
        def visit(self, url):
            self.request = urllib2.Request(self.url)
            self.response = urllib2.urlopen(self.request)
            return self.response.read()
    
    if __name__ == "__main__":
        x = Crawler()
        print x.visit("http://google.com/")
    

When I try to start getting the error:

    
    
    sigo@sarch ~/sources $ python test.py 
    Traceback (most recent call last):
      File "test.py", line 16, in <module>
        print x.visit("http://google.com/")
      File "test.py", line 10, in visit
        self.request = urllib2.Request(self.url)
    AttributeError: Crawler instance has no attribute 'url'
    

What am I doing wrong?


Answer: You're saying `self.url` which is referring to the Crawler class's `url`
attribute, which does not exist. You need to use just `url` since that's the
name of the variable from your `visit()` function arguments.

