No.Clue
No.Clue

Reputation: 85

Cannot extract href from a tags within span elements

I am trying to extract the href links (shown below) after extracting the span tags. However, it is throwing the following error:

Traceback (most recent call last): File "C:/Users/qeq981/Desktop/work.py", line 34, in print(element.find('a')['href']) TypeError: 'NoneType' object is not subscriptable

I am using the following code:
result2 = soup.find_all('span', {'style': 'white-space: nowrap'}) for element in result2: print(element) print(element.find('a')['href'])

However, if I omit the ['href'], I get all the span tags:

<span style="white-space: nowrap"><a href="https://www.mobygames.com/game/linux/americas-army-operations">Linux</a> (<em>2003</em>)</span> <a href="https://www.mobygames.com/game/linux/americas-army-operations">Linux</a> <span style="white-space: nowrap"><a href="https://www.mobygames.com/game/macintosh/americas-army-operations">Macintosh</a> (<em>2003</em>)</span> <a href="https://www.mobygames.com/game/macintosh/americas-army-operations">Macintosh</a> <span style="white-space: nowrap"><a href="https://www.mobygames.com/game/windows/americas-army-operations">Windows</a> (<em>2002</em>)</span> <a href="https://www.mobygames.com/game/windows/americas-army-operations">Windows</a>

How can I extract the href?

Upvotes: 0

Views: 77

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195438

With the HTML code you posted the script works, fine (so the error has to be elsewhere in your HTML code).

To be extra sure that you extract the correct tags, you can use CSS selector span[style="white-space: nowrap"] a[href] to select only <a> tags with "href=" under specified <span> tags.

For example (txt is snippet from your question):

from bs4 import BeautifulSoup

soup = BeautifulSoup(txt, 'html.parser')

for a in soup.select('span[style="white-space: nowrap"] a[href]'):
    print(a['href'])

Prints:

https://www.mobygames.com/game/linux/americas-army-operations
https://www.mobygames.com/game/macintosh/americas-army-operations
https://www.mobygames.com/game/windows/americas-army-operations

Upvotes: 2

Related Questions