Jim
Jim

Reputation: 1357

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

Upvotes: 4

Views: 11016

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123830

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.

Upvotes: 12

Related Questions