zoukankan      html  css  js  c++  java
  • python xml处理

    一、xml.etree.ElementTree(以下简称ET)

    ​ Python标准库中,提供了ET的两种实现。一个是纯Python实现的xml.etree.ElementTree,另一个是速度更快的C语言实现xml.etree.cElementTree。请记住始终使用C语言实现,因为它的速度要快很多,而且内存消耗也要少很多。如果你所使用的Python版本中没有cElementTree所需的加速模块,你可以这样导入模块:

    try:
        import xml.etree.cElementTree as ET
    except ImportError:
        import xml.etree.ElementTree as ET
    

    ​ 如果某个API存在不同的实现,上面是常见的导入方式。当然,很可能你直接导入第一个模块时,并不会出现问题。请注意,自Python 3.3之后,就不用采用上面的导入方法,因为ElemenTree模块会自动优先使用C加速器,如果不存在C实现,则会使用Python实现。因此,使用Python 3.3+的朋友,只需要import xml.etree.ElementTree即可。
    ​ 以country.xml为例,内容如下:

    <?xml version="1.0"?>
    <data>
        <country name="Liechtenstein">
            <rank updated="yes">2</rank>
            <year>2008</year>
            <gdppc>141100</gdppc>
            <neighbor name="Austria" direction="E"/>
            <neighbor name="Switzerland" direction="W"/>
        </country>
        <country name="Singapore">
            <rank updated="yes">5</rank>
            <year>2011</year>
            <gdppc>59900</gdppc>
            <neighbor name="Malaysia" direction="N"/>
        </country>
        <country name="Panama">
            <rank updated="yes">69</rank>
            <year>2011</year>
            <gdppc>13600</gdppc>
            <neighbor name="Costa Rica" direction="W"/>
            <neighbor name="Colombia" direction="E"/>
        </country>
    </data>
    

    1.1解析

    1.1.1调用 parse() 方法,返回解析树

    import xml.etree.ElementTree as ET
    
    tree = ET.parse("country.xml")  # <class 'xml.etree.ElementTree.ElementTree'>
    root = tree.getroot()           # 获取根节点 <Element 'data' at 0x02BF6A80>
    

    ​ 本质上和方法三相同,parse() 源码如下:

    def parse(source, parser=None):
        """Parse XML document into element tree.
    
        *source* is a filename or file object containing XML data,
        *parser* is an optional parser instance defaulting to XMLParser.
    
        Return an ElementTree instance.
    
        """
        tree = ElementTree()
        tree.parse(source, parser)
        return tree
    

    1.1.2 调用 from_string() ,返回解析树的根元素

    import xml.etree.ElementTree as ET
    data = open("country.xml").read()
    root = ET.fromstring(data)   # <Element 'data' at 0x036168A0>
    

    1.1.3 调用 ElementTree模块的 ElementTree(self, element=None, file=None)类 # 这里的element作为根节点

    import xml.etree.ElementTree as ET
    tree = ET.ElementTree(file="country.xml")  # <xml.etree.ElementTree.ElementTree object at 0x03031390>
    root = tree.getroot()  # <Element 'data' at 0x030EA600>
    

    1.2遍历

    1.2.1简单遍历

    import xml.etree.ElementTree as ET
    
    tree = ET.parse("country.xml")
    root = tree.getroot()
    print(root.tag, ":", root.attrib)  # 打印根元素的tag和属性
    
    # 遍历xml文档的第二层
    for child in root:
        # 第二层节点的标签名称和属性
        print("	" + child.tag,":", child.attrib)
        # 遍历xml文档的第三层
        for children in child:
            # 第三层节点的标签名称和属性
            print("		" + children.tag, ":", children.attrib)
    

    可以通过下标的方式直接访问节点

    # 访问根节点下第一个country的第二个节点year,获取对应的文本
    year = root[0][1].text    # 2008
    

    1.2.2ElementTree提供的方法

    • find(match)    # 查找第一个匹配的子元素, match可以时tag或是xpaht路径
    • findall(match) # 返回所有匹配的子元素列表
    • findtext(match, default=None) #
    • iter(tag=None) # 以当前元素为根节点 创建树迭代器,如果tag不为None,则以tag进行过滤
    • iterfind(match) #

    例子:

    # 过滤出所有neighbor标签
    for neighbor in root.iter("neighbor"):
        print(neighbor.tag, ":", neighbor.attrib)
    

    ---

    # 遍历所有的counry标签
    for country in root.findall("country"):
        # 查找country标签下的第一个rank标签
        rank = country.find("rank").text
        # 获取country标签的name属性
        name = country.get("name")
        print(name, rank)
    

    1.3 修改xml结构

    1.3.1 属性相关

    # 将所有的rank值加1,并添加属性updated为yes
    for rank in root.iter("rank"):
        new_rank = int(rank.text) + 1
        rank.text = str(new_rank)  # 必须将int转为str
        rank.set("updated", "yes") # 添加属性
    
    # 再终端显示整个xml
    ET.dump(root)
    # 注意 修改的内容存在内存中 尚未保存到文件中
    # 保存修改后的内容
    tree.write("output.xml")
    

    ---

    import xml.etree.ElementTree as ET
    
    tree = ET.parse("output.xml")
    root = tree.getroot()
    
    for rank in root.iter("rank"):
        # attrib为属性字典
        # 删除对应的属性updated
        del rank.attrib['updated']  
    
    ET.dump(root)
    

    小结: 关于class xml.etree.ElementTree.``Element 属性相关

    • attrib    # 为包含元素属性的字典
    • keys() # 返回元素属性名称列表
    • items() # 返回(name,value)列表
    • get(key, default=None) # 获取属性
    • set(key, value) # 跟新/添加 属性
    • del xxx.attrib[key] # 删除对应的属性

    1.3.2节点/元素 相关

    ​ 删除子元素remove()

    import xml.etree.ElementTree as ET
    
    tree = ET.parse("country.xml")
    root = tree.getroot()
    
    # 删除rank大于50的国家
    for country in root.iter("country"):
        rank = int(country.find("rank").text)
        if rank > 50:
            # remove()方法 删除子元素
            root.remove(country)
    
    ET.dump(root)
    

    ​ 添加子元素

    import xml.etree.ElementTree as ET
    
    tree = ET.parse("country.xml")
    root = tree.getroot()
    
    country = root[0]
    last_ele = country[len(list(country))-1]
    last_ele.tail = '
    		'
    # 创建新的元素, tag为test_append
    elem1 = ET.Element("test_append")
    elem1.text = "elem 1"
    # elem.tail = '
    	'
    country.append(elem1)
    
    # SubElement() 其实内部调用的时append()
    elem2 = ET.SubElement(country, "test_subelement")
    elem2.text = "elem 2"
    
    # extend()
    elem3 = ET.Element("test_extend")
    elem3.text = "elem 3"
    elem4 = ET.Element("test_extend")
    elem4.text = "elem 4"
    country.extend([elem3, elem4])
    
    # insert()
    elem5 = ET.Element("test_insert")
    elem5.text = "elem 5"
    country.insert(5, elem5)
    
    ET.dump(country)
    

    添加子元素方法总结:

    • append(subelement)
    • extend(subelements)
    • insert(index, element)

    1.4创建xml文档

    ​ 想创建root Element,然后创建SubElement,最后将root element传入ElementTree(element),创建tree,调用tree.write()方法写入文件

    ​ 对于创建元素的3个方法: 使用ET.Element、Element对象的makeelement()方法以及ET.SubElement

    import xml.etree.ElementTree as ET
    
    
    def subElement(root, tag, text):
        ele = ET.SubElement(root, tag)
        ele.text = text
        ele.tail = '
    '
    
    
    root = ET.Element("note")
    
    to = root.makeelement("to", {})
    to.text = "peter"
    to.tail = '
    '
    root.append(to)
    
    subElement(root, "from", "marry")
    subElement(root, "heading", "Reminder")
    subElement(root, "body", "Don't forget the meeting!")
    
    tree = ET.ElementTree(root)
    tree.write("note.xml", encoding="utf-8", xml_declaration=True)
    

    效果:

    img

      由于原生保存的XML时默认无缩进,如果想要设置缩进的话, 需要修改保存方式

    import xml.etree.ElementTree as ET
    from xml.dom import minidom
    
    
    def subElement(root, tag, text):
        ele = ET.SubElement(root, tag)
        ele.text = text
    
    
    def saveXML(root, filename, indent="	", newl="
    ", encoding="utf-8"):
        rawText = ET.tostring(root)
        dom = minidom.parseString(rawText)
        with open(filename, 'w') as f:
            dom.writexml(f, "", indent, newl, encoding)
    
    
    root = ET.Element("note")
    
    to = root.makeelement("to", {})
    to.text = "peter"
    root.append(to)
    
    subElement(root, "from", "marry")
    subElement(root, "heading", "Reminder")
    subElement(root, "body", "Don't forget the meeting!")
    
    # 保存xml文件
    saveXML(root, "note.xml")
    

    二、lxml

    2.1 Element 对象

    2.1.1 工厂函数

    ​ 在 XML或HTML 中每一处尖括号代表着一个标签或者元素, lxml 库为了方便操作, 封装了Element类, 通过Element对象可以很方便地操作 XML 的元素创建 Element 对象

    from lxml import etree
    
    # 创建根元素
    root = etree.Element("root")
    # 查看标签名
    print("root.tag: ", root.tag)
    
    >>>打印结果
    root.tag:  root
    

    ​ 添加 SubElement 对象, 组成有层级关系的 Elements

    # 直接添加
    root.append(etree.Element("child_1"))
    # 通过工厂函数
    child_2 = etree.SubElement(root, "child_2")
    child_3 = etree.SubElement(root, "child_3")
    

    ​ 将 Elements 序列化为 XML 树

    xml_s = etree.tostring(root, pretty_print=True).decode('utf-8')
    print("XML tree:", xml_s, sep='
    ')
    

    ​ 打印结果

    XML tree:
    <root>
      <child_1/>
      <child_2/>
      <child_3/>
    </root>
    

    ​ 没有子元素时, lxml 自动生成单标签

    ​ 这样我们直接通过 lxml 库创建了一个 XML 文档

    2.1.2 列表接口

    ​ 每一个 Element 对象相当于一个列表容器, 其内容为直接子元素, 操作方法与内置列表很相似

    2.1.2.1 索引

    child = root[0]
    print("root[0]:", child.tag)
    

    ​ 结果为首个添加的子元素

    2.1.2.2 切片

    # 切片 (仅包含 child_3)
    root_slice = root[-1:]
    print("slice:", type(root_slice))
    
    >>>
    slice: <class 'list'>
    

    ​ 切片的结果为内置列表

    2.1.2.3 列表方法

    # 查找
    idx = root.index(child)
    print("child idx:", idx)
    # 插入
    root.insert(0, etree.Element("child_0"))
    # 附加
    # root.append(etree.Element("child_1")) 前面已经试过了
    # 删除
    del root[-1]
    # 追加 (补回 child_3)
    root.extend(root_slice)
    
    
    >>>
    child idx: 0
    

    ​ 在 root 起始位置添加了 child_0, 随后删除了 child_3, 通过 extend 在末尾补回 child_3

    2.1.2.4 遍历

    for ele in root:
        print(ele.tag)
        
    >>>
    child_0
    child_1
    child_2
    child_3
    

    ​ Element 对象与列表有一个细小的差别, Element 列表成员之间赋值, lxml 会删除赋值元素

    root[0] = root[-1]
    for ele in root:
        print(ele.tag)
        
    >>>
    child_3
    child_1
    child_2
    

    ​ 最后一个元素的位置被删除了, 这么做是避免修改其中的一个元素, 另一个元素也会相应被修改

    2.1.3 元素关系

    ​ 父元素与相邻元素

    print("parent:", root is child.getparent())
    # root[1] 之前是 root[0]
    print("before:", root[0] is root[1].getprevious())
    # root[1] 之后是 root[2]
    print("after: ", root[2] is root[1].getnext())
    
    >>>
    parent: True
    before: True
    after:  True
    

    2.1.4 字典接口

    ​ 在 XML 或 HTML 中每一个标签都有属性, Element类通过字典接口支持属性操作

    ​ 创建带有属性的Element对象

    from lxml import etree
    
    # 带属性的 Element
    root = etree.Element("root", name="root", **{"class": "main"})
    # 序列化为 XML 标签
    root_s = etree.tostring(root).decode()
    print(root_s)
    
    >>>
    <root name="root" class="main"/>
    

    ​ 注意class是 Python 关键字, 因此无法使用class="main"按名称传参,而要用字典的方式

    2.1.4.1 访问属性

    # 访问已有属性
    print("name:", root.get("name"))
    # 没有相应属性时, 可指定缺省值
    print("title:", root.get("title", "element"))
    
    >>>
    name: root
    title: element
    

    ​ 与字典不同, 无法通过 root["name"] 访问和设置属性值, Element 的魔法方法 __getitem__ 不支持字符串索引

    2.1.4.2 设置属性

    # 设置属性
    root.set("id", "root-elem")
    

    2.1.4.3 遍历属性

    # 遍历键-值
    for k, v in root.items():
        # Python3.7 以上才支持 f-string
        print(f"{k}: {v}")
        
    >>>
    name: root
    class: main
    id: root-elem
    

    ​ 除了 items(), root 与字典一样还支持 keys()values()

    2.1.4.4 获取属性字典

    ​ 借助于property类, Element对象的attrib属性可视为字典对象

    # 将 Element 的 attrib 属性视为字典
    print("root.attrib", root.attrib)
    
    # 通过字符串索引修改和访问标签属性
    root.attrib["id"] = "root-attrib"
    print('root.attrib["id"]:', root.attrib["id"])
    
    # 通过 attrib 修改后, Element 也相应被修改
    root_s = etree.tostring(root).decode()
    print("root:", root_s)
    
    >>>
    root.attrib: {'name': 'root', 'class': 'main', 'id': 'root-elem'}
    root.attrib["id"]: root-attrib
    root: <root name="root" class="main" id="root-attrib"/>
    

    ​ 对 Element 元素属性的修改会映射到 attrib 属性, 反之亦然

    2.1.4.5 操作属性字典

    root.attribroot本身更接近字典

    # 更新属性
    root.attrib.update({"id": "root-update"})
    # 仅遍历值
    for v in root.attrib.values():
        print("attribute value:", v)
        
    >>>
    attribute value: root
    attribute value: main
    attribute value: root-update
    

    2.1.5添加文本内容

    ​ 如果将Element对象看做元素节点, 那么其包含的文本就可看做文本节点, 在 lxml 包中Element对象可以添加文本内容

    ​ 创建包含文本内容的Element对象

    from lxml import etree
    
    root = etree.Element("root")
    # 设置文本内容
    root.text = "Text"
    root_s = etree.tostring(root).decode()
    print("root:", root_s)
    
    >>>
    root: <root>Text</root>
    

    ​ 由于包含文本内容, root 变为双标签

    ​ 在 XML 文档中, 文本内容只能包含于双标签内, 而 HTML 则不同, 文本内容可位于不同的标签之间, 所以Element对象新增了tail属性, 可在Element 末尾新增文本内容

    html = etree.Element("html")
    body = etree.SubElement(html, "body")
    body.text = "BODY"
    span = etree.SubElement(body, "span")
    span.text = "SPAN_1"
    
    html_1 = etree.tostring(html, pretty_print=True).decode()
    print(html_1)
    # 在尾部新增内容
    span.tail = "SPAN_2"
    html_2 = etree.tostring(html, pretty_print=True).decode()
    print(html_2)
    
    >>>
    <html>
      <body>BODY<span>SPAN_1</span></body>
    </html>
    
    <html>
      <body>BODY<span>SPAN_1</span>SPAN_2</body>
    </html>
    

    ​ 在序列化 Element 对象时, 可以忽略末尾文本, 也可以仅输出文本内容

    # 忽略末尾文本
    html_3 = etree.tostring(html, with_tail=True, pretty_print=True).decode()
    print(html_3)
    # 仅文本内容
    html_4 = etree.tostring(html, method='text').decode()
    print(html_4)
    
    >>>
    <html>
      <body>BODY<span>SPAN_1</span>SPAN_2</body>
    </html>
    
    BODYSPAN_1SPAN_2
    

    2.2 xpath

    2.2.1 xpath 提取文本节点

    ​ 与 text() 不同, 功能函数 string() 表示递归提取元素的文本内容, 并将它们连接成一个文本

    ​ 创建 XML 示例

    from lxml import etree
    
    # 创建 Elements
    html = etree.Element("html")
    body = etree.SubElement(html, "body")
    body.text = "BODY"
    span = etree.SubElement(body, "span")
    span.text = "SPAN_1"
    span.tail = "SPAN_2"
    
    html_s = etree.tostring(html, pretty_print=True).decode()
    print(html_s)
    
    >>>
    <html>
      <body>BODY<span>SPAN_1</span>SPAN_2</body>
    </html>
    

    ​ 对比 text() 与 string()

    # 使用 //text() 提取所有文本节点
    print(html.xpath("//text()"))
    
    # 使用 string() 提取所有文本节点
    print(html.xpath("string()"))
    
    >>>
    ['BODY', 'SPAN_1', 'SPAN_2']
    BODYSPAN_1SPAN_2
    

    ​ 使用 html 调用 xpath 表示从 html 节点开始解析

    # string() 中使用参数, 仅寻找 span 的文本节点
    print(html.xpath("string(//span)"))
    # 寻找所有 body 的文本节点
    print(html.xpath("//body/text()"))
    
    >>>
    SPAN_1
    ['BODY', 'SPAN_2']
    

    ​ string() 中可以使用参数限制递归查找的范围, 仅寻找 html 下所有 span 元素的文本节点

    ​ 这里 span 元素末尾的文本内容 “SPAN_2” 被分给了 body 元素

    ​ 如果要经常使用某一个 XPath 表达式, 可以将解析规则保存到变量

    # 保存解析规则
    text_xpath = etree.XPath("//text()")
    # 解析 html 元素
    r = text_xpath(html)
    # 解析结果为列表
    print(r)
    # 查看解析后的文本类型
    r_0, r_1, r_2 = r
    print(type(r_0))
    
    >>>
    ['BODY', 'SPAN_1', 'SPAN_2']
    <class 'lxml.etree._ElementUnicodeResult'>
    

    ​ 在 lxml 包中, XPath 解析的结果通常为列表

    ​ 对于lxml.etree._ElementUnicodeResult这个类无需感到陌生, 因为它直接继承了str, 并新增了一些内容

    # 直接继承字符串
    print("r_0 -> str:", isinstance(r_0, str))
    # 可获取父元素
    p = r_0.getparent()
    # 父元素标签名
    print(p.tag)
    
    print("r_0 is text:", r_0.is_text)
    print("r_2 is tail:", r_2.is_tail)
    
    >>>
    r_0 -> str: True
    body
    r_0 is text: True
    r_2 is tail: True
    

    2.2.2XPath 提取属性节点

    创建 HTML 示例

    # 创建 Elements
    html = etree.Element("html")
    html.attrib["lang"] = "en"
    head = etree.SubElement(html, "head")
    meta = etree.SubElement(head, "meta")
    meta.set("charset", "UTF-8")
    etree.SubElement(head, "title").text = "Element_3"
    
    html_s = etree.tostring(html, pretty_print=True).decode()
    print(html_s)
    
    >>>
    <html lang="en">
      <head>
        <meta charset="UTF-8"/>
        <title>Element_3</title>
      </head>
    </html>
    

    @*表示匹配所有属性节点, 对比一下几种提取方法

    # 绝对路径-1
    charset_1 = html.xpath("/html/@*")
    # 绝对路径-2
    head = html[0]
    charset_2 = head.xpath("/html/@*")
    
    # 相对路径
    charset_3 = html.xpath("./@*")
    
    print(charset_1)
    print(charset_2)
    print(charset_3)
    
    >>>
    ['en']
    ['en']
    ['en']
    

    提取属性节点和文本节点的结果类型是一样的, 都是继承了内置字符串并新增了一些方法

    # 结果类型
    r = charset_1[0]
    print(type(r))
    # 父元素标签名
    print("parent's tag:", r.getparent().tag)
    # 是否为属性节点
    print("is_attribute:", r.is_attribute)
    # 属性名
    print("attrname:", r.attrname)
    
    >>>
    <class 'lxml.etree._ElementUnicodeResult'>
    parent's tag: html
    is_attribute: True
    attrname: lang
    
  • 相关阅读:
    cocos2dx的发展的例子2048(加入动画版)
    Hibernate操作Clob数据类型
    json级联城市
    Ubuntu Linux 永山(mount)分
    C++出现计算机术语5
    Cocos2d-x 3.0 红孩儿私人义务教育
    大页(huge pages) 三大系列 ---计算大页配置参数
    EJB_消息驱动发展bean
    HDU5086Revenge of Segment Tree(数论)
    第五章_JSTL
  • 原文地址:https://www.cnblogs.com/dongye95/p/13974466.html
Copyright © 2011-2022 走看看