I am new to lxml and HTML Parsers in general. I was wondering if there is a way to replace an element in the tree with another element ...
For example, I have:
body = """<code> def function(arg): print arg </code> Blah blah blah <code> int main() { return 0; } </code> """ doc = lxml.html.fromstring(body) codeblocks = doc.cssselect('code') for block in codeblocks: lexer = guess_lexer(block.text_content()) hilited = highlight(block.text_content(), lexer, HtmlFormatter()) doc.replace(block, hilited)
I want to do something along these lines, but this leads to a "TypeError" because "hilited" is not lxml.etree._Element.
Is it possible?
Hi,
python lxml
tsoporan
source share