假设我有这样一个 html 字符串:
<html>
<div id="d1">
Text 1
</div>
<div id="d2">
Text 2
<a href="http://my.url/">a url</a>
Text 2 continue
</div>
<div id="d3">
Text 3
</div>
</html>
我想提取d2
中NOT 被其他标签包裹的内容,跳过一个url
。换句话说,我想得到这样的结果:
Text 2
Text 2 continue
有没有办法用 BeautifulSoup 做到这一点?
我试过了,但是不正确:
soup = BeautifulSoup(html_doc, 'html.parser')
s = soup.find(id='d2').text
print(s)
最佳答案
尝试使用 .find_all(text=True, recursive=False)
:
from bs4 import BeautifulSoup
div_test="""
<html>
<div id="d1">
Text 1
</div>
<div id="d2">
Text 2
<a href="http://my.url/">a url</a>
Text 2 continue
</div>
<div id="d3">
Text 3
</div>
</html>
"""
soup = BeautifulSoup(div_test, 'lxml')
s = soup.find(id='d2').find_all(text=True, recursive=False)
print(s)
print([e.strip() for e in s]) #remove space
它将返回一个只有文本
的列表
:
[u'\n Text 2\n ', u'\n Text 2 continue\n ']
[u'Text 2', u'Text 2 continue']
关于python - 如何在 Python 中使用 BeautifulSoup 提取标签内的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44858226/