How to get HTML representation of a child element in BeautifulSoup?

Assuming my HTML is:

<html><body><span>This is my text</span></body></html>

How to get a string representation of what is contained inside, i.e.:

<span>This is my text</span>
+3
source share
1 answer

To get an html view of an element, simply use the built-in function str:

soup = BeautifulSoup("<html><body><span>This is my text</span></body></html>")
span = soup.find('span')
str(span) # Outputs '<span>This is my text</span>'
+9
source

All Articles