[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'

 

錯誤發生情形:python

xml模塊解析xml格式字符串的時候, 沒法調用getroot方法url

問題緣由是此處的fromstring直接返回的就是root, spa

 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 >>>

 

解決:code

單獨導入fromstring方法orm

1 from xml.etree.ElementTree import fromstring, ElementTree 2 tree = ElementTree(fromstring(<your_xml_string>))
相關文章
相關標籤/搜索