码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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")

  • 相关阅读:
    Java阻塞队列中的异类,SynchronousQueue底层实现原理剖析
    企业架构LNMP学习笔记56
    【论文速读,找找启发点】2024/6/16
    手把手教你使用Git管理你的软件代码
    操作系统——中断
    强网杯2022 pwn 赛题解析——house_of_cat
    喜大普奔!有软考电子版证书可以正式办理广州落户了!
    【微服务 从0开始 】Spring Cloud —— 比xml更适合的配置文件yaml
    【论文翻译】2.5PC:一个更快的非阻塞原子提交协议
    通过Node.js获取高德的省市区数据并插入数据库
  • 原文地址:https://blog.csdn.net/weixin_36670529/article/details/101985253
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号