144

I like very much the requests package and its comfortable way to handle JSON responses.

Unfortunately, I did not understand if I can also process XML responses. Has anybody experience how to handle XML responses with the requests package? Is it necessary to include another package for the XML decoding?

1
  • In case you use pydantic, you can rely on pydantic-xml to transform your XML data into a pydantic schema
    – Pynchia
    Oct 13, 2022 at 8:23

2 Answers 2

238

requests does not handle parsing XML responses, no. XML responses are much more complex in nature than JSON responses, how you'd serialize XML data into Python structures is not nearly as straightforward.

Python comes with built-in XML parsers. I recommend you use the ElementTree API:

import requests
from xml.etree import ElementTree

response = requests.get(url)

tree = ElementTree.fromstring(response.content)

or, if the response is particularly large, use an incremental approach:

response = requests.get(url, stream=True)

# if the server sent a Gzip or Deflate compressed response, decompress
# as we read the raw stream:
response.raw.decode_content = True

events = ElementTree.iterparse(response.raw)

for event, elem in events:
    # do something with `elem`

The external lxml project builds on the same API to give you more features and power still.

1
  • 1
    I always used lxml, didn't know this already exists in python built-in. Nov 12, 2020 at 15:14
34

A much simpler way is to convert the XML into a dict using the xmltodict package

response = requests.get('http://blabla.com')
dict_data = xmltodict.parse(response.content)

Now, dict_data it's just a Python dictionary.

You can install it with pip: pip install xmltodict

2
  • When I try to use this I get "ExpatError: not well-formed (invalid token): line 1, column 2". This is the beginning of the xml text: <?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet type="text/xsl" href="SettlementSummary.xsl"?> Feb 9, 2022 at 21:08
  • 1
    Hi @otterdog2000, I think your question lie outside the scope of this issue, you should open another one Feb 10, 2022 at 13:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.