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']
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With