How to fetch data from an XML file using URL in Python

← Prev

Here in this article I am going to show you how to fetch and process data from an XML file in Python. I am using VS-Code to run (execute) the example.

The XML is stored in a remote file and I'll be using the requests library in Python to make an http request. If the request is successful, it will respond with the XML document (I'll get the XML content), and next "parse" the XML data (or content) using xml.etree.ElementTree module.

parse xml from file in url in python

First you need to install the requests library. Because I am using VSCode, I can import requests module like this... from pip._vendor import requests.

Here's the example.

from pip._vendor import requests
import xml.etree.ElementTree as ET

def fetchLibrary ():
    try:
        response = requests.get("https://www.encodedna.com/library/library.xml")
        response.raise_for_status()  	# check if there are any http errors
        xmlData  = response.content
        
        showTheList(xmlData )	        # show xml data

    except requests.exceptions.RequestException as e:
        print(f"Failed to fetch data: {e}")

def showTheList(xmlData ):
    try:
        root = ET.fromstring(xmlData )
        book_list = root.findall('.//List')

        for book in book_list:
            book_name = book.find('BookName').text
            category = book.find('Category').text

            print(f"Book Name: {book_name}, Category: {category}")
    except ET.ParseError as e: 
        print(f"Failed to parse XML data: {e}")

fetchLibrary ()

Run the code and see the output in the "terminal" window.

python xml parsing output in terminal

xml.etree.ElementTree Module

The xml.etree.ElementTree module in Python, provides an API for parsing and creating XML data.

➡️ Learn more about xml.etree.ElementTree.

requests.get() Method

requests.get() method is part of the requests library that I am importing in the beginning of the example code.

The "requests.get()" method in Python, is used to send a GET request. The request is made using a URL. The URL is for the XML file, which is stored in a remote server.

ET.fromstring() Method

ET is the short for xml.etree.ElementTree. ET.fromstring() method parses the XML from a string into an element, which is the root element. You can check it like this.

root = ET.fromstring(xml_content)
print(root)

Next, I'll find all the "<List>" tag inside XML. See this library file again

Finally, I'll loop through all the tags and get the details.

← Previous