使用 BeautifulSoup 提取 <span> WITH 标签

Using BeautifulSoup to extract <span> WITH tags

如何使用 <br/> 标签正确提取 <span> 的值?

from bs4 import BeautifulSoup

html_text = '<span id="spamANDeggs">This is<br/>what<br/>I want. WITH the <br/> tags.</span>'

soup = BeautifulSoup(html_text)

text_wanted = soup.find('span',{'id':'spamANDeggs'}).GetText(including<br/>...)

你可以像这样使用decode_contents()方法:

from bs4 import BeautifulSoup

html_text = '<span id="spamANDeggs">This is<br/>what<br/>I want. WITH the <br/> tags.</span>'
soup = BeautifulSoup(html_text)
text_wanted = soup.find('span', {'id': 'spamANDeggs'}).decode_contents(formatter="html")

现在 text_wanted 等于 "This is<br/>what<br/>I want. WITH the <br/> tags."