I’m trying to use HTML data inside an the text node of an element, but it gets
encoded as if it were meant to not be HTML data.
Here is an MWE:
from xml.etree import ElementTree as ET
data = '<a href="https://example.com">Example data gained from elsewhere.</a>'
p = ET.Element('p')
p.text = data
p = ET.tostring(p, encoding='utf-8', method='html').decode('utf8')
print(p)
The output is…
<p><a href="https://example.com">Example data gained from elsewhere.</a></p>
What I intended is…
<p><a href="https://example.com">Example data gained from elsewhere.</a></p>
>Solution :
You can parse the HTML string into an ElementTree object and append it to the DOM:
from xml.etree import ElementTree as ET
data = '<a href="https://example.com">Example data gained from elsewhere.</a>'
p = ET.Element('p')
p.append(ET.fromstring(data))
p = ET.tostring(p, encoding='utf-8', method='html').decode('utf8')
print(p)