Reputation: 1706
How can I properly extract the value of a <span>
WITH the <br/>
tags?
i.e.
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/>...)
Upvotes: 0
Views: 661
Reputation: 122
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."
Upvotes: 4