python:function just exactly 1 argument(2 given) [英] python: function takes exactly 1 argument (2 given)

查看:281
本文介绍了python:function just exactly 1 argument(2 given)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类中有此方法

class CatList:

    lista = codecs.open('googlecat.txt', 'r', encoding='utf-8').read()
    soup = BeautifulSoup(lista)    
    # parse the list through BeautifulSoup
    def parseList(tag):
        if tag.name == 'ul':
            return [parseList(item)
                    for item in tag.findAll('li', recursive=False)]
        elif tag.name == 'li':
            if tag.ul is None:
                return tag.text
            else:
                return (tag.contents[0].string.strip(), parseList(tag.ul))

但是当我尝试调用它:

myCL = CatList()
myList = myCL.parseList(myCL.soup.ul)

我有以下错误:

parseList() takes exactly 1 argument (2 given) 

作为方法的参数,但是当我这样做时,我得到的错误是以下:

I tried to add self as an argument to the method but when I do that the error I get is the following:

global name 'parseList' is not defined 

我不清楚这是如何工作的。

not very clear to me how this actually works.

任何提示?

感谢

推荐答案

忘记了 self 参数。

您需要更改此行:

def parseList(tag):

def parseList(self, tag):

你也有一个全局名错误,因为你试图访问 parseList 没有 self

虽然你应该这样做:

You also got a global name error, since you're trying to access parseList without self.
While you should to do something like:

self.parseList(item)

具体来说,你需要在代码的两行中这样做:

To be specific, you need to do that in two lines of your code:

 return [self.parseList(item)

 return (tag.contents[0].string.strip(), self.parseList(tag.ul))

这篇关于python:function just exactly 1 argument(2 given)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆