9

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'] 
1
  • 2
    The string attribute of a is None. You need to look over the documentation for BeautifulSoup and see what .find_all() returns. Commented Dec 17, 2016 at 22:53

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Good catch. Though I think the main problem is that OP is searching titles within all the links (.find_all("a")).
I would agree with that. I think that using a more selective search would help OP more, as my code papers over the problem.
6

From BS4 documentation:

If a tag contains more than one thing, then it’s not clear what .string should refer to, so .string is defined to be None

I believe you can use .text to get what you want:

title = a.text.strip() 

4 Comments

it sometimes throws 'str' object has no attribute 'text'
@fartwhif, this can never happen with OP's code. If you have that issue, you can ask a question on StackOverflow with your code.
fair enough, I'm very much a python noob so I don't doubt you hahahah
@fartwhif - I got the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.