How do I rename a node using LXML?
Specifically, how to rename a parent node i.e. a <body>
tag while preserving all the underlying structure?
I am parsing using the lxml.html
module but supposedly there shouldn't be any difference between xml and html in terms of renaming between lxml.html.HtmlElement
and its XML counterpart.
I have searched through the docs at the LXML site but didn't find any reference to renaming of nodes.
Once you have the <body>
element, just change its tag
attribute.
import lxml.etree
import lxml.html
doc = lxml.html.fromstring("<html><body><p></body></html>")
body = doc.find('body')
body.tag = "body-not"
print(lxml.etree.tostring(doc))
This prints
b'<html><body-not><p/></body-not></html>'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With