How can I properly extract the value of a <span>
WITH the <br/>
tags?
ie
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/>...)
You can use decode_contents()
method just like this:
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")
Now text_wanted
equals "This is<br/>what<br/>I want. WITH the <br/> tags."
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.