jkp jkp - 8 months ago 45
HTML Question

How can I instantiate a comment element programatically using lxml?

I'm using lxml to programatically build HTML and I need to include a custom comment in the output. Whilst there is code in lxml to cope with comments (they can be instantiated when parsing existing HTML code) I cannot find a way to instantiate one programatically.

Can anyone help?


You can use the lxml.etree.Comment() factory function. It will return a comment element that you can use like any other element.