double_j
double_j

Reputation: 1706

Using BeautifulSoup to extract <span> WITH tags

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

Answers (1)

suvari3V
suvari3V

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

Related Questions