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?