本文介绍了Python 入门:属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,今天刚下载.我用它来处理网络蜘蛛,所以为了测试它并确保一切正常,我下载了一个示例代码.不幸的是,它不起作用并给了我错误:

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' 对象没有属性 'loaded'"

"AttributeError: 'MyShell' object has no attribute 'loaded' "

我不确定代码本身是否有错误,或者我在安装 python 时没有正确执行某些操作.安装python时有什么需要做的吗,比如添加环境变量等?该错误通常意味着什么?

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)

推荐答案

Python 中的属性是属于对象的名称 - 方法或变量.AttributeError 表示程序尝试使用对象的属性,但该对象没有请求的属性.

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.

例如,字符串对象具有 'upper' 属性,这是一种返回字符串大写版本的方法.您可以编写一个像这样使用它的方法:

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()

但是,请注意该方法中没有任何内容可以确保您 给它一个字符串.您可以传入一个文件对象或一个数字.两者都没有 'upper' 属性,Python 会引发属性错误.

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.

这篇关于Python 入门:属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 10:21