Easy way to get data between tags of xml or html files in python?

后端 未结 6 795
时光取名叫无心
时光取名叫无心 2021-02-06 17:05

I am using Python and need to find and retrieve all character data between tags:

I need this stuff

I then want to output

6条回答
  •  终归单人心
    2021-02-06 17:16

    I quite like parsing into element tree and then using element.text and element.tail.

    It also has xpath like searching

    >>> from xml.etree.ElementTree import ElementTree
    >>> tree = ElementTree()
    >>> tree.parse("index.xhtml")
    
    >>> p = tree.find("body/p")     # Finds first occurrence of tag p in body
    >>> p
    
    >>> p.text
    "Some text in the Paragraph"
    >>> links = p.getiterator("a")  # Returns list of all links
    >>> links
    [, ]
    >>> for i in links:             # Iterates through all found links
    ...     i.attrib["target"] = "blank"
    >>> tree.write("output.xhtml")
    

提交回复
热议问题