如何用美丽的汤获得标签的最内层文本



我有以下html元素:

<blockquote class="abstract">
<span class="descriptor"> abstract</span>
Abstract text goes here
</blockquote>

我有兴趣获得"绝对文本..."。我在python和beautifulsoup中尝试了以下方法。

abstract=soup.find('blockquote', {"class":'abstract mathjax'})

以上是正确的(我检查了打印它(。但是以下都没有成功获得文本:

print abstract.text
print abstract.find(text=True)
print abstract.get_text()

有什么线索吗?提前非常感谢你,

加布里埃尔

您正在尝试查找abstractmathjax。请尝试以下操作:

from bs4 import BeautifulSoup
html = """<blockquote class="abstract">
<span class="descriptor"> abstract</span>
Abstract text goes here
</blockquote>"""    
soup = BeautifulSoup(html, "html.parser")
abstract = soup.find('blockquote', class_='abstract')
abstract.span.extract()   # Remove span element
print abstract.text

这将打印:

Abstract text goes here

最新更新