简体   繁体   中英

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

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM