0
我努力学习Python中的类:Python的履带 - AttributeError的:履带式实例没有属性 'URL'
#!/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/")
当我尝试启动正在错误:
[email protected] ~/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'
我究竟做错了什么?
感谢您的编辑!它确实是类实例的'url'属性(即'self.url'),它不会退出,而函数局部变量'url'就是这样做的。 – bakennedy