I'm trying to make a div element from the below string with html entities. Since my string contains html entities, & reserved char in the html entity is being escaped as & in the output. Thus html entities are displayed as plain text. How can I avoid this so html entities are rendered properly?
s = 'Actress Adamari López And Amgen Launch Spanish-Language Chemotherapy: Myths Or Facts™ Website And Resources'
div = etree.Element("div")
div.text = s
lxml.html.tostring(div)
output:
<div>Actress Adamari L&#243;pez And Amgen Launch Spanish-Language Chemotherapy: Myths Or Facts&#8482; Website And Resources</div>
You can specify
encodingwhile callingtostring():As a side note, you should definitely use
lxml.html.tostring()while dealing withHTMLdata:Also see: