Python Crawler - AttributeError:Crawler 实例没有属性“url”;
我正在尝试学习 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/")
当我尝试开始收到错误时:
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'
我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所说的
self.url
指的是 Crawler 类的url
属性,该属性不存在。您只需使用url
,因为这是visit()
函数参数中的变量名称。You're saying
self.url
which is referring to the Crawler class'surl
attribute, which does not exist. You need to use justurl
since that's the name of the variable from yourvisit()
function arguments.