jkp jkp - 5 months ago 20
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?

Answer

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

Comments