繁体   English   中英

无法从 BeautifulSoup4 中的字符串解析“href”

[英]Cannot Parse 'href' From String In BeautifulSoup4

我在这里有这个代码片段:

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)

这个想法是从这个字符串中解析href。

但是,我不明白为什么它看不到它。 我的 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'

为什么 BeautifulSoup 看不到这个字符串的href?

当您尝试使用i["href"]访问href时,您会像访问它一样访问它dict ,但事实并非如此。 您必须首先使用.find()方法找到标签。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM