Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Etree tag creation of lxml in python

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the creation of etree tags in lxml in python". In daily operation, I believe that many people have doubts about the creation of etree tags in lxml in python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "the creation of etree tags in lxml in python". Next, please follow the editor to study!

First, install pip install lxml II, create the label from lxml import etreeroot = etree.Element ('root') 3, add the child node from lxml import etreeroot = etree.Element (' root') span = etree.SubElement (root, 'span') 4, delete the child node from lxml import etreeroot = etree.Element (' root') span = etree.SubElement (root 'span') root.remove (span) 5. Delete all child nodes from lxml import etreeroot = etree.Element (' root') root.clear () 6. Operate child nodes from lxml import etreeroot = etree.Element ('root') span = etree.SubElement (root,' span') # get the number of tags len (root) # get the tag index number, if there are multiple identical tags, you can distinguish root.index (span) # insert root.insert by location (0 Etree.Element ('p')) # add root.append (etree.Element ('strong')) to the tail. 7. Get the parent node

There are two ways to get the parent node of a tag:

From lxml import etreeroot = etree.Element ('root') span = etree.SubElement (root,' span') # get the parent node method-span.getparent () .tag# get the parent node method two, get the child node with the list, then get the parent node root [0] .getparent () .tag8, create the attribute from lxml import etreeroot = etree.Element ('root') root.set (' title', 'this is a root tag') IX, get the attribute

Three ways to get attributes

From lxml import etreeroot = etree.Element ('root') # get attribute method 1 root.get (' title') # get attribute method 2, refer to dictionary operations root.keys (), root.values (), root.items () # get attribute method 3, directly get the dictionary root.attrib stored in the attribute, and set the label text

Add text and append text

From lxml import etreeroot = etree.Element ('root') # tag followed by the text root.tail =' I am autofelix' 11, xpath method from lxml import etreeroot = etree.Element ('root') word = root.xpath (' / / text ()') word [0] .getparent () .tag 12, Determine whether the text type from lxml import etreeroot = etree.Element ('root') word = root.xpath (' / / text ()') # is text text word.is_text# is tail text word.is_ tail13, string parsing from lxml import etreehtml = etree.fromstring ('autofelix') html.tagetree.tostring (html) 14, XML parsing from lxml import etreehtml = etree.XML (' autofelix') html.tagetree.tostring (html) 15, Remove the blank line from lxml import etree# in XML remove the blank line parser= etree.XMLParser (remove_blank_text=True) root = etree.XML ('auto felix') in the xml file Parser) print etree.tostring (root) XVI, HTML parsing

HTML method, if there is no and tag, it will be automatically added.

From lxml import etreehtml = etree.HTML ('autofelix') etree.tostring (html) XVII. Search and locate from lxml import etreeroot = etree.XML (' I am autofelix') # findall operation returns list root.findall ('a') [0] .text # find operation is equivalent to finding this element node Return the first matching element root.find ('. / / a') .text# match list parsing [b.text for b in root.findall ('. / / a')] # query root.findall ('. / a [@ class]') [0] .tag here according to the attribute The study on "etree tag creation of lxml in python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report