Fork me on GitHub

[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:\WINDOWS\system32> 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>))

 

posted @ 2018-02-07 15:58  [sigai]  阅读(7805)  评论(0编辑  收藏  举报