The requests module in Python is a popular library for making HTTP requests. It can be used to send HTTP requests and parse the response, whether it is in JSON, HTML, or another format.
To parse an XML response using the requests module, you will need to use a module that is capable of parsing XML data.
One popular option is the xml.etree.ElementTree module, which is part of the Python standard library.
Here is an example of how to use the requests and xml.etree.ElementTree modules to parse an XML response:
Copy code
import requests
import xml.etree.ElementTree as ET
response = requests.get('https://example.com/data.xml')
# Check the status code to ensure the request was successful
if response.status_code == 200:
# Parse the XML data using ElementTree
root = ET.fromstring(response.text)
# Iterate over the XML elements and print their values
for child in root:
print(child.tag, child.text)
else:
print('Request failed with status code:', response.status_code)
This example sends an HTTP GET request to https:
It iterates over the XML elements and prints their tag and value.
If you need to parse more complex XML data, you may need to use additional functions and methods provided by the xml.etree.ElementTree module. Consult the documentation for more information.