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?
You’re saying
self.urlwhich is referring to the Crawler class’surlattribute, which does not exist. You need to use justurlsince that’s the name of the variable from yourvisit()function arguments.