I’m using ElementTree to generate some HTML, but I’ve run into the problem that ElementTree doesn’t store text as a Node, but as the text and tail properties of Element. This is a problem if I want to generate something that would require multiple text nodes, for example:
<a>text1 <b>text2</b> text3 <b>text4</b> text5</a>
As far as I can tell there is no way to generate this- am I missing something? Or, is there a better solution for quick and simple HTML generation in Python?
To generate the above string with
ElementTreeyou can use the following code. The trick to this is that thetextis the very first lot of text before the next element and thetailis all the text after the element up to the next element.