zoukankan      html  css  js  c++  java
  • lxml.etree 教程6:Tree iteration

    Elements provide a tree iterator for this purpose. It yields elements in document order, i.e. in the order their tags would appear if you serialised the tree to XML:

    >>> root = etree.Element("root")
    >>> etree.SubElement(root, "child").text = "Child 1"
    >>> etree.SubElement(root, "child").text = "Child 2"
    >>> etree.SubElement(root, "another").text = "Child 3"
    
    >>> print(etree.tostring(root, pretty_print=True))
    <root>
      <child>Child 1</child>
      <child>Child 2</child>
      <another>Child 3</another>
    </root>
    
    >>> for element in root.iter():
    ...     print("%s - %s" % (element.tag, element.text))
    root - None
    child - Child 1
    child - Child 2
    another - Child 3
    

     If you know you are only interested in a single tag, you can pass its name to iter() to have it filter for you. Starting with lxml 3.0, you can also pass more than one tag to intercept on multiple tags during iteration.

    >>> for element in root.iter("child"):
    ...     print("%s - %s" % (element.tag, element.text))
    child - Child 1
    child - Child 2
    
    >>> for element in root.iter("another", "child"):
    ...     print("%s - %s" % (element.tag, element.text))
    child - Child 1
    child - Child 2
    another - Child 3
    

     By default, iteration yields all nodes in the tree, including ProcessingInstructions, Comments and Entity instances. If you want to make sure only Element objects are returned, you can pass the Element factory as tag parameter:

    >>> root.append(etree.Entity("#234"))
    >>> root.append(etree.Comment("some comment"))
    
    >>> for element in root.iter():
    ...     if isinstance(element.tag, basestring):
    ...         print("%s - %s" % (element.tag, element.text))
    ...     else:
    ...         print("SPECIAL: %s - %s" % (element, element.text))
    root - None
    child - Child 1
    child - Child 2
    another - Child 3
    SPECIAL: ê - ê
    SPECIAL: <!--some comment--> - some comment
    
    >>> for element in root.iter(tag=etree.Element):
    ...     print("%s - %s" % (element.tag, element.text))
    root - None
    child - Child 1
    child - Child 2
    another - Child 3
    
    >>> for element in root.iter(tag=etree.Entity):
    ...     print(element.text)
    ê
    

     Note that passing a wildcard "*" tag name will also yield all Element nodes (and only elements).

    In lxml.etree, elements provide further iterators for all directions in the tree: children, parents (or rather ancestors) and siblings.

  • 相关阅读:
    腾讯云环境配置之PHP5.6.3 + redis扩展 稳定版
    越狱后的ios如何用apt-get 安装各种命令
    批量 kill mysql 中运行时间长的sql
    谷歌Chrome浏览器开发者工具的基础功能
    话说好像是这样,ios下面通常用iframe来打开你的scheme地址; Android下通常用location.href来。。。 不过实际情况好像比这个复杂得多。。
    js判断移动端是否安装某款app的多种方法
    设计不错的网站
    BADIP filter
    开窗函数 函数() OVER()
    2018年1月初的一次面试题
  • 原文地址:https://www.cnblogs.com/bluescorpio/p/3131213.html
Copyright © 2011-2022 走看看