How to rename a node with Python LXML?

2024/9/20 7:51:43

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.

Answer

Once you have the <body> element, just change its tag attribute.

import lxml.etree
import lxml.htmldoc = 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>'
https://en.xdnf.cn/q/72373.html

Related Q&A

In python, selenium, how do I set the error messages for a wait?

I have the following code:WebDriverWait(self.driver, 20).until(expected_conditions.element_to_be_clickable(click))Now this sometimes fails and I know why it fails. But the error gives me TimeoutExcept…

Python: the mechanism behind list comprehension

When using list comprehension or the in keyword in a for loop context, i.e:for o in X:do_something_with(o)orl=[o for o in X]How does the mechanism behind in works? Which functions\methods within X do…

PRAW: Comment Submitters Username

Im developing a reddit bot that needs to know which user submitted a comment. According to the PRAW API wrapper docs, theres no specific way to get the username of a Comment objects author. Ideally I c…

Paramiko, exec_command get the output stream continuously [duplicate]

This question already has answers here:Get output from a Paramiko SSH exec_command continuously(6 answers)Closed 2 years ago.I dry on a Python script. I create a python script for a given IP will conne…

pdfminer3k has no method named create_pages in PDFPage

Since I want to move from python 2 to 3, I tried to work with pdfmine.3kr in python 3.4. It seems like they have edited everything. Their change logs do not reflect the changes they have done but I had…

curve fitting zipf distribution matplotlib python

I tried to fit the following plot(red dot) with the Zipf distribution PDF in Python, F~x^(-a). I simply chose a=0.56 and plotted y = x^(-0.56), and I got the curve shown below. The curve is obviously …

Running python/ruby script on iPhone?

From the recent news from the Apple, I learned that one has to use C/C++/Objective-C for iPhone App. Accordingly, its not possible to use MacPython or similar to make iPhone App. But as the python/ruby…

Unexpected behavior of universal newline mode with StringIO and csv modules

Consider the following (Python 3.2 under Windows):>>> import io >>> import csv >>> output = io.StringIO() # default parameter newline=None >>> csvdata = [1, …

logger chain in python

Im writing python package/module and would like the logging messages mention what module/class/function they come from. I.e. if I run this code:import mymodule.utils.worker as workerw = worker.Worker()…

How to make data to be shown in tabular form in discord.py?

Hi I am creating a bot that makes points table/leaderboard , below is the code which works really nice. def check(ctx):return lambda m: m.author == ctx.author and m.channel == ctx.channelasync def get_…