简体   繁体   English

Python TypeError:切片索引必须是整数或无或具有 __index__ 方法

[英]Python TypeError: slice indices must be integers or None or have an __index__ method

I'm trying to pull some datas, i have a function to get all tags in an array after i return it.我正在尝试提取一些数据,我有一个函数可以在返回数组后获取数组中的所有标签。

My code :我的代码:

def getfontCats(soup):
    cats_name = soup.find("div", {"class": ["fontTagsMain"]}).find_all("a")
    cat_list = []
    for cats in cats_name:
        cat_list.append(cats.get_text())

    return cat_list


for sk in set(getListing(soup)):
    print(sk)
    print(getfontCats(sk))
    print("###################################################")
    time.sleep(1)

HTML Content (Soup): HTML 内容(汤):

<div class="fontTagsMain">
<a href="/animal-fonts.php" class="fontTag">Animal</a><a href="/comic-cartoon-fonts.php" class="fontTag">Comic Cartoon</a><a href="/cartoon-fonts.php" class="fontTag">Cartoon</a><a href="/comic-fonts.php" class="fontTag">Comic</a><a href="/fun-fonts.php" class="fontTag">Fun</a><a href="/funny-fonts.php" class="fontTag">Funny</a><a href="/comic-book-fonts.php" class="fontTag">Comic Book</a> </div>

Output :输出 :

Traceback (most recent call last):
  File "/Users/evilslab/Documents/Websites/www.futurepoint.dev.cc/dobuyme/socket/fonts.py", line 109, in <module>
    print(getfontCats(sk))
  File "/Users/evilslab/Documents/Websites/www.futurepoint.dev.cc/dobuyme/socket/fonts.py", line 40, in getfontCats
    cats_name = soup.find("div", {"class": ["fontTagsMain"]}).find_all("a")
TypeError: slice indices must be integers or None or have an __index__ method

But it works when i use the code which i used inside function outside.但是当我使用我在函数内部使用的代码时它会起作用。 But when i try to call the code with function it gives me error.但是当我尝试使用函数调用代码时,它给了我错误。

Yes fixed.是固定的。 I'm sorry i was initialised soup in function对不起,我在函数中初始化了汤

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 typeerror slice索引必须为整数或不为整数,或具有__index__方法 - typeerror slice indices must be integers or none or have an __index__ method 在 Python 中使用字符串作为切片索引? (类型错误:切片索引必须是整数或无或具有 __index__ 方法) - Use strings as slice indices in Python ? (TypeError: slice indices must be integers or None or have an __index__ method) Python - 切片索引必须是整数或无或具有 __index__ 方法 - Python - slice indices must be integers or None or have an __index__ method TypeError:切片索引必须为整数或无,或具有__index__方法python - TypeError: slice indices must be integers or None or have an __index__ method python 切片索引必须为整数或无,或具有__index__方法 - Slice indices must be integers or None or have __index__ method 切片索引必须是整数或无或有 __index__ 方法错误? - Slice indices must be integers or None or have an __index__ method error? TypeError:切片索引必须是整数或无或具有 __index__ 方法(书本示例不起作用) - TypeError: slice indices must be integers or None or have an __index__ method (Book example not working) Python返回TypeError:切片索引必须为整数或无,或者从字符串字典中搜索键时具有__index__方法 - Python returns TypeError: slice indices must be integers or None or have an __index__ method when searching for a key from a dictionary in a string TypeError:切片索引必须为整数或None或具有__index__方法。 怎么解决呢? - TypeError: slice indices must be integers or None or have an __index__ method. How to resolve it? TypeError:切片索引必须为整数或无,或具有__index__方法NLP - TypeError: slice indices must be integers or None or have an __index__ method NLP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM