[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 ...
随机推荐
- Ubuntu管理软件源
在Ubuntu环境下,我们经常会使用apt-get(apt)命令下载各种软件,当所需软件在官方软件库中找不到时,我们需要添加第三方的软件源,或者由于位于海外的官方软件源下载速度过于感人时,需要添加国内 ...
- SpringBoot2 整合 Zookeeper组件,管理架构中服务协调
本文源码:GitHub·点这里 || GitEE·点这里 一.Zookeeper基础简介 1.概念简介 Zookeeper是一个Apache开源的分布式的应用,为系统架构提供协调服务.从设计模式角度来 ...
- 奇葩的Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
启动springboot的时候莫名其妙出现这个错误,我properties里面也没配置数据源啥的,但就是出现这个错误 解决方法: 在启动类上加@SpringBootApplication(exclud ...
- invalid expression: missing ) after argument list in xxx 或者 console.error(("[Vue warn]: " + msg + trace));
效果图: 此处错误原因 中文输入法的 逗号 导致 : 解决方案: 改为 英文输入法的 逗号
- python的break、continue、pass
break break可以用来立即退出循环语句(包括else)continue continue可以用来跳过当次循环注意:break和continue都是只对离他最近的循环起作用 pass pass是 ...
- js正则定义支付宝账号、手机号、邮箱
一.支付宝账号:可以只输入数字.字母.字母(数字)+数字(字母),其中只字母中可以含有@._或者.也可以三者都可以包含并且可以在任意位置,限制:小于等于30位(可根据需求自定义范围): let zh ...
- bzoj 2683 CDQ分治
题目描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 将格子x,y里的数 ...
- 洛谷 P1463 [POI2002][HAOI2007]反素数
题目链接 题目描述 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数1, ...
- http请求头中的content-type属性
在HTTP请求中,我们每天都在使用Content-Type来指定不同格式的请求信息,但是却很少有人去全面了解Content-Type中允许的值有多少,因此这里来了解一下Content-Type的可用值 ...
- .net core webapi搭建(3)Code first+拆层三层+仓储
将项目拆层 我们要 将项目拆分成 Infrastructure 基础层 Core 核心层 Utility 工具 我们想在就 ...