Getting Started with Python: Attribute Error

Deadly 提交于 2021-02-16 14:49:06

问题


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)

回答1:


And what does that error generally mean?

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:

def get_upper(my_string):
  return my_string.upper()

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.




回答2:


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.



来源:https://stackoverflow.com/questions/2767607/getting-started-with-python-attribute-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!