Skip to content
Advertisement

Nonetype error/ No elements printed using beautifulsoup for python

So im trying to compare 2 lists using python, one contains like 1000 links i fetched from a website. The other one contains a few words, that might be contained in a link in the first list. If this is the case, i want to get an output. i printed that first list, it actually works. for example if the link is “https://steamcdn-a.swap.gg/apps/730/icons/econ/stickers/eslkatowice2015/counterlogic.f49adabd6052a558bff3fe09f5a09e0675737936.png” and my list contains the word “eslkatowice2015”, i want to get an output using the print() function. My code looks like this:

page_source = driver.page_source

soup = BeautifulSoup(page_source, 'lxml')
Bot_Stickers = soup.find_all('img', class_='csi')

for sticker in Bot_Stickers:

    for i in StickerIDs:

        if i in sticker:

            print("found")
driver.close()

now the problem is that i dont get an output, which is impossible because if i manually compare the lists, there are clearly elements from the first list existing in the 2nd list (the one with the links). when trying to fix i always got a NoneType error. The driver.page_source is above defined by some selenium i used to access a site and click some javascript stuff, to be able to find everything. I hope its more or less clear what i wanted to reach

Edit: the StickerIDs variable is the 2nd list containing the words i want to be checked

Advertisement

Answer

NoneType error means that you might be getting a None somewhere, so it’s probably safer to check the results returned by find_all for None.

It’s been a while since is used BeautifulSoup, but If I remember correctly, find_all returns a list of beautiful soup tags that match the search criteria, not URLs. You need to get the href attribute from the tag before checking if it contains a keyword.

Something like that:

page_source = driver.page_source

soup = BeautifulSoup(page_source, 'lxml')
Bot_Stickers = soup.find_all('img', class_='csi')

if Bot_Stickers and StickersIDs:
 
    for sticker in Bot_Stickers:
        for i in StickerIDs:
            if i in sticker.get("href"): # get href attribute of the img tag
                print("found")
else:
    print("Bot_Stickers:", Bot_Stickers)
    print("StickersIDs:" StickersIDs)

driver.close()
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement