Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element Tree : Can't build root tree when getting XML from webpage

I'm trying to parse an XML page I'm downloading from the web.

import requests
url = "http://www.w3schools.com/xml/cd_catalog.xml"
XML = requests.get(url)
print XML.content

tree = ET.ElementTree(XML)
root = tree.getroot()

print root.tag, root.attrib

I get one of two errors when I try and do this

for the above example webpage AttributeError: 'Response' object has no attribute 'tag'

And for the actualy XML site I'm looking at AttributeError: 'str' object has no attribute 'tag'

However if I just copy and paste the XML I've downloaded into a .xml file and open it works fine with no errors. Would anyone know how to fix these problems..?

like image 417
Jim Avatar asked Dec 21 '22 04:12

Jim


1 Answers

You need to parse the response body, not the response object:

root = ET.fromstring(XML.content) # no .getroot() call required

or pass in a file object:

XML = requests.get(url, stream=True)
tree = ET.parse(XML.raw)
root = tree.getroot()

The latter could fail if the stream is compressed; the raw file object does not decompress this for you.

like image 139
Martijn Pieters Avatar answered Feb 02 '23 00:02

Martijn Pieters