Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'NoneType' object has no attribute 'strip'

Tags:

python

I've been trying to learn Python (currently requests and beautifulsoup4) and I found a tutorial online

The issue is I keep getting the below error and cannot figure it out at all...

Any help would be appreciated!

Traceback (most recent call last): File "C:\Users\BillyBob\Desktop\Web Scrap.py", line 14, in title = a.string.strip() AttributeError: 'NoneType' object has no attribute 'strip'

Here is my code in case I made a mistake;

import requests
from bs4 import BeautifulSoup

result = requests.get("http://www.oreilly.com/")

c = result.content

soup = BeautifulSoup(c, "html.parser")
samples = soup.find_all("a")
samples[0]

data = {}
for a in samples:
    title = a.string.strip()
    data[title] = a.attrs['href']
like image 246
Ctsa3 Avatar asked Jan 05 '23 22:01

Ctsa3


1 Answers

The first member of samples does not have a string attribute, and as a result, a.string doesn't return anything, so you're calling the strip() method on something that doesn't exist.

However, then you have another problem; it is not necessarily true that a has the href attribute. Instead, you should check explicitly for both, or else you will get errors (which is the problem with Yevhen's answer, which is otherwise correct).

One potential fix to your problem is to write:

for a in samples:
    if not a.string is None:
        title = a.string.strip()
        if 'href' in a.attrs.keys():
            data[title] = a.attrs['href']

This way, you check explicitly for each parameter before calling the associated method.

like image 188
finbarr Avatar answered Jan 18 '23 04:01

finbarr