0

I'm trying to retrieve data from an API, however it appears to be returning in XML format.

response = requests.get('https string')
print(response.text)

Output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><RegisterSearch TotalResultsOnPage="500" TotalResults="15167" TotalPages="31" PageSize="500" CurrentPage="1"><SearchResults><Document DocumentId="1348828088640186163"/><Document DocumentId="1348828088751561003"/></SearchResults></RegisterSearch>

I've tried using ElementTree as suggested by other answers, but receive a file not found error. I think I'm missing something.

import xml.etree.ElementTree as ET
tree = ET.parse(response.text)
root = tree.getroot()
1
  • 1
    response.text is a string, not a file. Use ET.fromstring(). docs.python.org/3/library/… Commented Jun 1, 2022 at 15:23

1 Answer 1

1

EDIT:

If you want to use ElementTree You need to parse from STRING

root = ET.fromstring(response.text)

You can parse it with Beautiful Soup

from bs4 import BeautifulSoup
soup = BeautifulSoup(response.text, 'xml')

Then depending on what you want to find or extract you can use find

soup.find('DocumentId').text
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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