Rever92
Rever92

Reputation: 19

Python 'NoneType' Object Has No Attribute 'attrs'

i am testing out Python a little and have been following some lessons, but i am quite stuck at this point:

urls = []
for h3_tag in soup.find_all("h3"):
    a_tag = h3_tag.find('a')
    urls.append(a_tag.attrs['href'])
print(urls)

This is supposed to get the "a" inside a set of h3. And it does, but when i then add the .attrs['href'] or the .text in order to get the URL Anchor or the URL itself i keep getting this error: AttributeError: 'NoneType' object has no attribute 'attrs'

I cant seem to work it out...

Thanks in advance

Upvotes: 0

Views: 1465

Answers (2)

Darien Schettler
Darien Schettler

Reputation: 586

As mentioned by chitown88 you should ensure that h3_tag.find('a') does not return None.

However, you should avoid doing it with unrestricted try and except statements. It can make troubleshooting difficult in the future. An alternative to my version would be to simply place a KeyError after the except clause. i.e. except KeyError:

See here for more details


Here is my preferred way to handle this

urls = []

for h3_tag in soup.find_all("h3"):
    # Get the a-tag or set a_tag to None if no a-tag is found
    a_tag = h3_tag.find('a')

    # Guarantee that we were able to find an a-tag
    if a_tag:        
        # Guarantee that the a_tag has an `href` attribute
        if a.get('href'):
            urls.append(a_tag.attrs['href'])

print(urls)

I hope this helps!

Upvotes: 2

chitown88
chitown88

Reputation: 28630

if it doesn't find an a tag, then you can't get the href attribute, if it even has one. I'd incorporate a try/except here then, or as stated in the comment, check if it's None

urls = []
for h3_tag in soup.find_all("h3"):
    try:
        a_tag = h3_tag.find('a')
        urls.append(a_tag.attrs['href'])
    except:
        continue
print(urls)

Upvotes: 0

Related Questions