8

How can I save XML API response to an .xml files? Or cache it, that way I can parse it before parsing the response.

import requests
r = requests.get('url',  auth=('user', 'pass'))

I looked at a similar question for JSON: https://stackoverflow.com/a/17519020/4821590

import requests
import json
solditems = requests.get('https://github.com/timeline.json') # (your url)
data = solditems.json()
with open('data.json', 'w') as f:
    json.dump(data, f)
2

2 Answers 2

22

If you want to be able to parse the returned XML before doing stuff with it, the xml tree is your friend.

import requests
import xml.etree.ElementTree as ET

r = requests.get('url',  auth=('user', 'pass'))
tree = ET.parse(r.text)
root = tree.getroot()

Otherwise, as jordanm has commented, you could just save it to a file and be done with it.

with open('data.xml', 'w') as f:
    f.write(r.text)
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, I'm looking at the documentation now, really helpful. Sorry, don't have the reputation to upvote you.
@JulioMontes if the answer is sufficient, you may mark it as accepted by clicking the tick next to the voting buttons. If you need extra help or hints, comment below and I'll help you out. Or you can wait it out a bit to see what other answers come up :)
Well, I'm going to keep this open (not sure if thats allowed) and do some digging myself and see how much further I get, don't want it all handed to me, I appreciate it though.
I was wondering if there is a way for me to get the response and be able to choose what to print. I know when its tree = ET.parse(r.text) the response is the whole XML. However, I want to be selective. I tried just using tree = ET.parse(r) but I must be missing something.
@juliomontes do you have an example? Do you mean like a particular child tag for instance?
|
13

Few notes related to Python3 (at least 3.6 versions):

1) when using xml.etree.ElementTree with requests, you use fromstring not parse. r.text returns a string, and xml.etree.ElementTree.parse is for files

import requests
import xml.etree.ElementTree as ET

r = requests.get("https://xml.returning.uri")
root = ET.fromstring(r.text)

2) This creates an element object as the root (no more tree). So to write it back out, you'll need to make it a tree:

tree = ET.ElementTree(root)
tree.write("file.xml")

From the docs

xml.etree.ElementTree.parse(source, parser=None) Parses an XML section into an element tree. source is a filename or file object containing XML data.

xml.etree.ElementTree.fromstring(text) Parses an XML section from a string constant. Same as XML(). text is a string containing XML data. Returns an Element instance

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.