Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a node with Python LXML?

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.

like image 677
ccpizza Avatar asked Apr 06 '16 18:04

ccpizza


1 Answers

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>'
like image 157
tdelaney Avatar answered Nov 06 '22 16:11

tdelaney