Logo

dev-resources.site

for different kinds of informations.

Working with XML in Python Requests library

Published at
12/22/2024
Categories
python
xml
requests
parsing
Author
xinitd
Categories
4 categories in total
python
open
xml
open
requests
open
parsing
open
Author
6 person written this
xinitd
open
Working with XML in Python Requests library

What is XML? XML mean Extensible Markup Language, which need for storing structured data and group any items. In XML markup language you may create tags with any names. The most popular examples of XML - Sitemaps and RSS feeds.

Example of XML file:

<breakfast_menu>
   <food>
       <name>Belgian Waffles</name>
       <price>$5.95</price>
       <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
       <calories>650</calories> </food>
   <food>
       <name>Strawberry Belgian Waffles</name>
       <price>$7.95</price>
       <description>Light Belgian waffles covered with strawberries and whipped cream</description>
       <calories>900</calories> </food>
   <food>
       <name>Berry-Berry Belgian Waffles</name>
       <price>$8.95</price>
       <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
       <calories>900</calories> </food>
   <food>
       <name>French Toast</name>
       <price>$4.50</price>
       <description>Thick slices made from our homemade sourdough bread</description>
       <calories>600</calories> </food>
   <food>
       <name>Homestyle Breakfast</name>
       <price>$6.95</price>
       <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
       <calories>950</calories> </food>
</breakfast_menu>
Enter fullscreen mode Exit fullscreen mode

In this example, file contain breakfast_menu global tag, who include food categories, and every food category includes name, price, description and calories tag.

Now we start learning how to work with XML and Python Requests library. First we need to prepare our working environment.

For create new project and virtual environment install python3-virtualenv package. It need for separation requirements of each project. Installation in Debian/Ubuntu:

sudo apt install python3 python3-virtualenv -y
Enter fullscreen mode Exit fullscreen mode

Create project folder:

mkdir my_project
cd my_project
Enter fullscreen mode Exit fullscreen mode

Create Python virtual environment with env named folder:

python3 -m venv env
Enter fullscreen mode Exit fullscreen mode

Activate virtual environment:

source env/bin/activate
Enter fullscreen mode Exit fullscreen mode

Install PIP's dependencies:

pip3 install requests
Enter fullscreen mode Exit fullscreen mode

Let's start code writing.

Create main.py file and insert code below:

import requests
import xml.etree.ElementTree as ET
request = requests.get('https://www.w3schools.com/xml/simple.xml')
root = ET.fromstring(request.content)
for item in root.iter('*'):
    print(item.tag)
Enter fullscreen mode Exit fullscreen mode

This code snippet help us to find all inner tags.

The output of this code:

(env) user@localhost:~/my_project$ python3 main.py
breakfast_menu
food
name
price
description
calories
food
name
price
description
calories
food
name
price
description
calories
food
name
price
description
calories
food
name
price
description
calories
Enter fullscreen mode Exit fullscreen mode

Now we are write code for getting values from inner elements. Open main.py file and replace previously code with this:

import requests
import xml.etree.ElementTree as ET
request = requests.get('https://www.w3schools.com/xml/simple.xml')
root = ET.fromstring(request.content)
for item in root.iterfind('food'):
    print(item.findtext('name'))
    print(item.findtext('price'))
    print(item.findtext('description'))
    print(item.findtext('calories'))
Enter fullscreen mode Exit fullscreen mode

We received next result:

(env) user@localhost:~/my_project$ python3 main.py
Belgian Waffles
$5.95
Two of our famous Belgian Waffles with plenty of real maple syrup
650
Strawberry Belgian Waffles
$7.95
Light Belgian waffles covered with strawberries and whipped cream
900
Berry-Berry Belgian Waffles
$8.95
Light Belgian waffles covered with an assortment of fresh berries and whipped cream
900
French Toast
$4.50
Thick slices made from our homemade sourdough bread
600
Homestyle Breakfast
$6.95
Two eggs, bacon or sausage, toast, and our ever-popular hash browns
950
Enter fullscreen mode Exit fullscreen mode

At the final step we prettifying output data to make it easier to read:

import requests
import xml.etree.ElementTree as ET
request = requests.get('https://www.w3schools.com/xml/simple.xml')
root = ET.fromstring(request.content)
for item in root.iterfind('food'):
    print('Name: {}. Price: {}. Description: {}. Calories: {}'.format(item.findtext('name'), item.findtext('price'), item.findtext('description'), item.findtext('calories')))
Enter fullscreen mode Exit fullscreen mode

Here output:

(env) user@localhost:~/my_project$ python3 main.py
Name: Belgian Waffles. Price: $5.95. Description: Two of our famous Belgian Waffles with plenty of real maple syrup. Calories: 650
Name: Strawberry Belgian Waffles. Price: $7.95. Description: Light Belgian waffles covered with strawberries and whipped cream. Calories: 900
Name: Berry-Berry Belgian Waffles. Price: $8.95. Description: Light Belgian waffles covered with an assortment of fresh berries and whipped cream. Calories: 900
Name: French Toast. Price: $4.50. Description: Thick slices made from our homemade sourdough bread. Calories: 600
Name: Homestyle Breakfast. Price: $6.95. Description: Two eggs, bacon or sausage, toast, and our ever-popular hash browns. Calories: 950
Enter fullscreen mode Exit fullscreen mode

Source materials:
Example of XML file taken from W3Schools.


Are my posts is helpful? You may support me on Patreon.

xml Article's
30 articles in total
Favicon
Working with XML in Python Requests library
Favicon
XSD Tools in .NET8 – Part4 – XsdExe- Advanced
Favicon
XSD Tools in .NET8 – Part9 – LiquidXMLObjects- Simple
Favicon
XSD Tools in .NET8 – Part8 – LinqToXsdCore - Advanced
Favicon
XSD Tools in .NET8 – Part6 – XmlSchemaClassGenerator - Advanced
Favicon
XSD Tools in .NET8 – Part1 – VS2022
Favicon
XSD Tools in .NET8 – Part2 – C# validation
Favicon
XSD Tools in .NET8 – Part7 – LinqToXsdCore - Simple
Favicon
XSD Tools in .NET8 – Part5 – XmlSchemaClassGenerator - Simple
Favicon
XSD Tools in .NET8 – Part10 – LiquidXMLObjects - Advanced
Favicon
XSD Tools in .NET8 – Part3 – XsdExe- Simple
Favicon
Hacking Excel Files in Power Automate
Favicon
Passing an Array of Items to a SQL Stored Procedure Using XML from C#
Favicon
Power Automate - Handling XML
Favicon
From XML to Word: simplifying conversion with FileConversionLibrary
Favicon
Free and Powerful XML Formatting and Conversion Tool: XMLFormatter.online
Favicon
JSON vs. XML: The Advantages and Efficiency in Data Handling
Favicon
Boas práticas com XPATH
Favicon
Transforming Industries: The Benefits of XML EDI
Favicon
Update JSON file using Terminal or bash script
Favicon
Handling XML Data in R: A Step-by-Step Guide to Reading, Converting, and Parsing ❗❗
Favicon
Release 0.9.0 of `@xmldom/xmldom`
Favicon
ข้อมูล XML บน Postgres : มารู้จักรุ่นพี่ของวงการแลกเปลี่ยนข้อมูลระหว่าง service กัน
Favicon
Constructing XML output with dream-html
Favicon
How to Add xml sitemap in Magento 2
Favicon
How It Differs from HTML and Differences
Favicon
JSON vs. XML: Navigating the Data Exchange Landscape for Developers
Favicon
Evolving API Architectures: Exploring the GraphQL vs. REST Debate for Developers
Favicon
Bridging the Gap: Leveraging XML for Seamless Legacy Data Integration
Favicon
Unlocking the Fundamentals of XML: A Comprehensive Guide

Featured ones: