2015-04-02 70 views
0

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

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/>...) 

回答

3

您可以使用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."

相关问题