【发布时间】:2015-10-21 14:29:09
【问题描述】:
我有这个 html 结构:
<div class="foo">
<h3>Title</h3>
<br>Some text I want to retrieve. <br><br> This text too.
<br> (numbers and position of "br" tag indetermined) And this one too.
<div class="subfoo">Some other text I don't want.</div>
</div>
在我的python脚本中,我写了:
exampleSoup = bs4.BeautifulSoup(res.text, "html.parser")
elems = exampleSoup.select('.foo')
print(elems[0].getText())
不出所料,我得到了全文:
Title
Some text I want to retrieve.
Some other text I don't want.
如何仅获取 div 中没有标签的字符串,即:“我想检索一些文本。这个文本也是。还有这个。”? 感谢您的帮助。
【问题讨论】:
标签: python beautifulsoup data-extraction