Cannot Parse 'href' From String In BeautifulSoup4

Viewed 43

I have this code snippet here:

from bs4 import BeautifulSoup

myString = '<a href="/number-stations/german/g06" title="G06">G06</a>'

i = BeautifulSoup(str(myString), 'html.parser')
print(type(i))
print(i)
myText = i.get_text(strip=True)
print(myText)
myURL = i["href"]
print(myURL)

The idea is to parse the href from this string.

However, I cannot understand why it cant see it. My output:

<class 'bs4.BeautifulSoup'>
<a href="/number-stations/german/g06" title="G06">G06</a>
G06
Traceback (most recent call last):
  File "c:\Users\user\Desktop\aaa\test.py", line 10, in <module>
    myURL = i["href"]
  File "C:\ProgramData\Anaconda3\lib\site-packages\bs4\element.py", line 1401, in __getitem__
    return self.attrs[key]
KeyError: 'href'

Why cant BeautifulSoup see the href of this string?

1 Answers

When your trying to access the href using i["href"], your accessing it as if it's a dict, but it's not. You have to first find the tag using the .find() method.

from bs4 import BeautifulSoup

myString = '<a href="/number-stations/german/g06" title="G06">G06</a>'

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

print(soup.find('a').attrs)
print('-' * 10)
print(soup.find('a')['href'])

Output:

{'href': '/number-stations/german/g06', 'title': 'G06'}
----------
/number-stations/german/g06
Related