6

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.

ccpizza
  • 28,968
  • 18
  • 162
  • 169

1 Answers1

16

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>'
tdelaney
  • 73,364
  • 6
  • 83
  • 116