zoukankan      html  css  js  c++  java
  • [xml]AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getroot'

     1 >>> import requests
     2 >>> res = requests.get("https://xxx.com/sitemap.xml")
     3 >>> from xml.etree import cElementTree as ET
     4 >>> tree = ET.fromstring(res.text)
     5 >>> tree
     6 <Element '{http://www.sitemaps.org/schemas/sitemap/0.9}urlset' at 0x00000249FF7B9548>
     7 >>> tree.getroot()
     8 Traceback (most recent call last):
     9   File "<stdin>", line 1, in <module>
    10 AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getroot'

    错误发生情形:

    xml模块解析xml格式字符串的时候, 无法调用getroot方法

    问题原因是此处的fromstring直接返回的就是root, 

     1 >>> from xml.etree.ElementTree import fromstring, ElementTree
     2 >>> tree = ElementTree(fromstring(a))
     3 >>> tree.getroot()
     4 <Element '{http://www.sitemaps.org/schemas/sitemap/0.9}urlset' at 0x0000024980A19818>
     5 >>> exit()
     6 C:WINDOWSsystem32> python
     7 Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
     8 Type "help", "copyright", "credits" or "license" for more information.
     9 >>> import requests
    10 >>> res = requests.get("https://xxx.com/sitemap.xml")
    11 >>> from xml.etree import cElementTree as ET
    12 >>> root = ET.fromstring(res.text)
    13 >>> root
    14 <Element '{http://www.sitemaps.org/schemas/sitemap/0.9}urlset' at 0x00000274FAF49548>
    15 >>>

    解决:

    单独导入fromstring方法

    1 from xml.etree.ElementTree import fromstring, ElementTree
    2 tree = ElementTree(fromstring(<your_xml_string>))
  • 相关阅读:
    js 判断window操作系统 2种方法
    HTML5 16进制颜色
    html5 动画运动 属性
    html5 动画运动 属性
    html5 图片旋转 --位置定位
    html 5 过渡 属性 高度 宽度 颜色 样式等。。。
    jquery 文档操作
    html5 表单 自带验证
    PHP微信授权登录信息
    接口测试-requests高级用法
  • 原文地址:https://www.cnblogs.com/sigai/p/8427082.html
Copyright © 2011-2022 走看看