[xml]AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getroot'
>>> import requests
>>> res = requests.get("https://xxx.com/sitemap.xml")
>>> from xml.etree import cElementTree as ET
>>> tree = ET.fromstring(res.text)
>>> tree
<Element '{http://www.sitemaps.org/schemas/sitemap/0.9}urlset' at 0x00000249FF7B9548>
>>> tree.getroot()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getroot'
错误发生情形:
xml模块解析xml格式字符串的时候, 无法调用getroot方法
问题原因是此处的fromstring直接返回的就是root,
>>> from xml.etree.ElementTree import fromstring, ElementTree
>>> tree = ElementTree(fromstring(a))
>>> tree.getroot()
<Element '{http://www.sitemaps.org/schemas/sitemap/0.9}urlset' at 0x0000024980A19818>
>>> exit()
C:\WINDOWS\system32> python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> res = requests.get("https://xxx.com/sitemap.xml")
>>> from xml.etree import cElementTree as ET
>>> root = ET.fromstring(res.text)
>>> root
<Element '{http://www.sitemaps.org/schemas/sitemap/0.9}urlset' at 0x00000274FAF49548>
>>>
解决:
单独导入fromstring方法
from xml.etree.ElementTree import fromstring, ElementTree
tree = ElementTree(fromstring(<your_xml_string>))
[xml]AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getroot'的更多相关文章
- python 之xml.etree.ElementTree
Element类型是一种灵活的容器对象,用于在内存中存储结构化数据. [注意]xml.etree.ElementTree模块在应对恶意结构数据时显得并不安全. 每个element对象都具有以下属性: ...
- Python 标准库之 xml.etree.ElementTree
Python 标准库之 xml.etree.ElementTree Python中有多种xml处理API,常用的有xml.dom.*模块.xml.sax.*模块.xml.parser.expat模块和 ...
- 读取xml时,报错:xml.etree.ElementTree.ParseError: no element found: line 20, column 9
读取xml时,出现报错:xml.etree.ElementTree.ParseError: no element found: line 20, column 9 原因是xml文件格式有问题,可以检查 ...
- python模块:xml.etree.ElementTree
"""Lightweight XML support for Python. XML is an inherently hierarchical data format, ...
- python模块之xml.etree.ElementTree
xml.etree.ElementTree用于解析和构建XML文件 <?xml version="1.0"?> <data> <country nam ...
- xml.etree.ElementTree对CDATA的输出
xml.etree.ElmentTree不支持CDATA 的输出,但是支持Comment的输出.由于在项目中需要输出带有CDATA块的XML文本,参考Comment的做法,修改ElmentTree中的 ...
- python标准库xml.etree.ElementTree的bug
使用python生成或者解析xml的方法用的最多的可能就数python标准库xml.etree.ElementTree和lxml了,在某些环境下使用xml.etree.ElementTree更方便一些 ...
- python xml.etree.ElementTree模块
使用的XML文件如下:file.xml <?xml version="1.0"?> <data name="ming"> <cou ...
- python xml包 xml.etree.ElementTree使用记录
19.7.1 教程 这是一个简短的教程使用xml.etree.ElementTree(简称为et).目标是展示一些构建模块和模块的基本概念 9.7.1.1. XML tree and elements ...
随机推荐
- CF1200D White Lines | 前缀和
传送门 Examples input 1 4 2 BWWW WBBW WBBW WWWB output 1 4 input 2 3 1 BWB WWB BWB output 2 2 input 3 5 ...
- Go语言教程之结构体
Hello,大家好,我是小栈君,最近因为工作的事情延误了一点分享的进度,但是我会尽量抽时间分享关于IT干货知识,还希望大家能够持续关注"IT干货栈"哦. 闲话不多说,今天给大家继续 ...
- GitHub项目绑定自己的域名
github博客搭建:https://blog.csdn.net/walkerhau/article/details/77394659?utm_source=debugrun&utm_medi ...
- UGUI之MaskableGraphic
MaskableGraphic继承自Graphic,并且继承了IClippable, IMaskable, IMaterialModifier三个接口.它是RawImage.Image和Text的父类 ...
- selenium中的xpath用法,使用xpath定位元素
xpath路径选择器定位元素 1. xpath: 使用路径表达式来定位xml或者html中文档中选取节点.在 XPath 中,有七种类型的节点:元素.属性.文本.命名空间.处理指令.注释以及文档节点( ...
- NPOI读取Excel的数据
首先是给项目安装NPOI.DLL :Install-Package NPOI -Version 2.4.1 HttpPostedFile upLoadPostFile = FileUpload1.Po ...
- unbuntu18.04安装启用splash
官网:https://splash.readthedocs.io/en/stable/ 1.安装Docker https://www.cnblogs.com/wt7018/p/11880666.htm ...
- 自建CDN Xnign产品指标
Xnign-X1 Xnign-X1 性能参数 参考值 L7 HTTP RPS (128并发请求) 250W QPS L7 HTTP CPS (128并发请求) 110W QPS L7 HTTP RPS ...
- ssm项目中中文字符乱码
昨天给同学改一个错,在SSM项目中,表单输入的String类型中,中文字符值,总是乱码,在控制器层获取的数据就开始乱码,先后进行了如下排查: web.xml中配置设置字符编码的监听器(过滤器), js ...
- Unity SurfaceShader详解
声明:文章主要是总结手游开发的经验,只涉及到了前向渲染.未涉及延迟渲染. Unity的Surface Shader本质上就是VS/PS.只不过Unity经过精心设计,将shader划分为了几个关键部分 ...