• ET.parse().getroot()


    参考  ET.parse().getroot() - 云+社区 - 腾讯云

    getroot()

    Returns the root element for this tree.

    iter(tag=None)

    Creates and returns a tree iterator for the root element. The iterator loops over all elements in this tree, in section order. tag is the tag to look for (default is to return all elements).

    iterfind(match, namespaces=None)

    Same as Element.iterfind(), starting at the root of the tree.

    New in version 3.2.

    parse(source, parser=None)

    Loads an external XML section into this element tree. source is a file name or file object. parser is an optional parser instance. If not given, the standard XMLParser parser is used. Returns the section root element.

    write(file, encoding="us-ascii", xml_declaration=None, default_namespace=None, method="xml", *, short_empty_elements=True)

    Writes the element tree to a file, as XML. file is a file name, or a file object opened for writing. encoding 1 is the output encoding (default is US-ASCII). xml_declaration controls if an XML declaration should be added to the file. Use False for never, True for always, None for only if not US-ASCII or UTF-8 or Unicode (default is None). default_namespace sets the default XML namespace (for “xmlns”). method is either "xml", "html" or "text" (default is "xml"). The keyword-only short_empty_elements parameter controls the formatting of elements that contain no content. If True (the default), they are emitted as a single self-closed tag, otherwise they are emitted as a pair of start/end tags.

    The output is either a string (str) or binary (bytes). This is controlled by the encoding argument. If encoding is "unicode", the output is a string; otherwise, it’s binary. Note that this may conflict with the type of file if it’s an open file object; make sure you do not try to write a string to a binary stream and vice versa.

    New in version 3.4: The short_empty_elements parameter.

    This is the XML file that is going to be manipulated:

    1. <html>
    2. <head>
    3. <title>Example pagetitle>
    4. head>
    5. <body>
    6. <p>Moved to <a href="http://example.org/">example.orga>
    7. or <a href="http://example.com/">example.coma>.p>
    8. body>
    9. html>

    Example of changing the attribute “target” of every link in first paragraph:

    1. >>> from xml.etree.ElementTree import ElementTree
    2. >>> tree = ElementTree()
    3. >>> tree.parse("index.xhtml")
    4. <Element 'html' at 0xb77e6fac>
    5. >>> p = tree.find("body/p") # Finds first occurrence of tag p in body
    6. >>> p
    7. <Element 'p' at 0xb77ec26c>
    8. >>> links = list(p.iter("a")) # Returns list of all links
    9. >>> links
    10. [<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>]
    11. >>> for i in links: # Iterates through all found links
    12. ... i.attrib["target"] = "blank"
    13. >>> tree.write("output.xhtml")

  • 相关阅读:
    链表oj题 && 链表与LinkedList && 栈的概念 && 队列的概念 && 树和二叉树
    Adaboost 算法【python,机器学习,算法】
    【网络安全】安全事件管理处置 — 应急响应简介
    JMeter-BeanShell预处理程序和BeanShell后置处理程序的应用
    20231016比赛总结
    Scrapy使用GitHub上的ProxyPool代理池
    容器编排器们的自我介绍
    win7 64位安装vs code
    js_typeof关键字和比较运算符
    SpringCloud微服务---Nacos配置中心
  • 原文地址:https://blog.csdn.net/weixin_36670529/article/details/101985253