[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 ...
随机推荐
- 傅立叶变换—FFT(cuda实现)
背景: 无意间看到cuda解决FFT有一个cufft函数库,大体查看了有关cufft有关知识,写了一个解决一维情况的cuda代码,据调查知道cufft在解决1D,2D,3D的情况时间复杂度都为O(nl ...
- 小白学 Python 爬虫(36):爬虫框架 Scrapy 入门基础(四) Downloader Middleware
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- Date类与日期格式
Date类概述: 表示特定的瞬间,精确到毫秒. Date()分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒).Date(long date)分配 Date 对象并初始化此对象,以表 ...
- docker学习1--dockerfile
记录docker学习过程 实践启动一个redis server 命令之前 要弄懂 dockfile 镜像 容器 三者概念以及三者之间的关系 dockerfile格式 # Comment 注释 INST ...
- .Net Core Web Api实践(四)填坑连接Redis时Timeout performing EVAL
前言:前两篇文章.net core+Redis+IIS+nginx实现Session共享中,介绍了使用Microsoft.Extensions.Caching.Redis实现Session共享的方法, ...
- 谈谈Java的Collection接口
目录 谈谈Collection 前言 Collection 方法 1.boolean add(E) 2.void clear() 3.boolean contains(Object o) 4.bool ...
- VMware Workstation CentOS7 Linux 学习之路(2)--.net core环境安装
1.为了安装.NET,需要注册微软签名密钥和添加微软相关的支持.这个操作每台机器只能做一次. Add the dotnet product feed(其实就是向微软提交投名状,表示我这台服务器要用co ...
- Spring Framework 源码编译导入
预先准备环境 Window 10 JDK环境 List item Gradle 以及其环境变量配置 spring-framework源码(https://gitee.com/mirrors/Sprin ...
- 大厂面试中三次握手延伸出来n连发你受得了?
目录 一.这是一次有故事的对话 二.三次握手的客户端服务端状态 1 先画个图看看有哪些状态 2 tcp协议内容解析 3 通过工具wireshark来验证我们所述 三.说下Linux网络编程常用API ...
- MySQL 排名、分组后组内排名、取各组的前几名
一.排名 /*普通排名:从1开始,顺序往下排*/ AS rank ) r ORDER BY score; /*并列排名:相同的值是相同的排名*/ SELECT cs.* , CASE WHEN @p= ...