Python 入门:属性错误
我是 python 新手,今天刚刚下载。我正在使用它来处理网络蜘蛛,因此为了对其进行测试并确保一切正常,我下载了示例代码。不幸的是,它不起作用并给我错误:
“AttributeError:'MyShell'对象没有属性'loaded'”
我不确定代码本身是否有错误或者我在安装python时未能正确执行某些操作。安装python的时候需要做什么比如添加环境变量等吗?该错误通常意味着什么?
这是我与导入的蜘蛛类一起使用的示例代码:
import chilkat
spider = chilkat.CkSpider()
spider.Initialize("www.chilkatsoft.com")
spider.AddUnspidered("http://www.chilkatsoft.com/")
for i in range(0,10):
success = spider.CrawlNext()
if (success == True):
print spider.lastUrl()
else:
if (spider.get_NumUnspidered() == 0):
print "No more URLs to spider"
else:
print spider.lastErrorText()
# Sleep 1 second before spidering the next URL.
spider.SleepMs(1000)
I am new to python and just downloaded it today. I am using it to work on a web spider, so to test it out and make sure everything was working, I downloaded a sample code. Unfortunately, it does not work and gives me the error:
"AttributeError: 'MyShell' object has no attribute 'loaded' "
I am not sure if the code its self has an error or I failed to do something correctly when installing python. Is there anything you have to do when installing python like adding environmental variables, etc.? And what does that error generally mean?
Here is the sample code I used with imported spider class:
import chilkat
spider = chilkat.CkSpider()
spider.Initialize("www.chilkatsoft.com")
spider.AddUnspidered("http://www.chilkatsoft.com/")
for i in range(0,10):
success = spider.CrawlNext()
if (success == True):
print spider.lastUrl()
else:
if (spider.get_NumUnspidered() == 0):
print "No more URLs to spider"
else:
print spider.lastErrorText()
# Sleep 1 second before spidering the next URL.
spider.SleepMs(1000)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Python 中的属性是属于对象(方法或变量)的名称。 AttributeError 意味着程序尝试使用对象的属性,但该对象不具有所请求的属性。
例如,字符串对象具有“upper”属性,该属性是返回字符串的大写版本的方法。您可以编写一个像这样使用它的方法:
但是,请注意,该方法中没有任何内容可以确保您必须为其提供一个字符串。您可以传入文件对象或数字。它们都没有“upper”属性,Python 将引发属性错误。
至于您为什么会在这种情况下看到它,您没有提供足够的详细信息供我们解决。将完整的错误消息添加到您的问题中。
An Attribute in Python is a name belonging to an object - a method or a variable. An AttributeError means that the program tried to use an attribute of an object, but the object did not have the requested attribute.
For instance, string objects have the 'upper' attribute, which is a method that returns the uppercase version of the string. You could write a method that uses it like this:
However, note that there's nothing in that method to ensure that you have to give it a string. You could pass in a file object, or a number. Neither of those have the 'upper' attribute, and Python will raise an Attribute Error.
As for why you're seeing it in this instance, you haven't provided enough detail for us to work it out. Add the full error message to your question.
1) 将代码放入 Try ... except 块中。获取异常详细信息。
2) 你能告诉 StackTrace 详细信息意味着哪一行和方法抛出错误吗
?你是否能够运行其他简单的 python 脚本而不会出现任何错误。意味着只是尝试运行一些示例脚本等。
1) Put code in Try ... Except block. get exception details.
2) Could you tell StackTrace details means which line # and method thrown error
And also are you able to run other simple python scripts without any error. Means just try to run some sample script etc.