What is XML? XML refers to Extensible Markup Language, which is required to store structured data and group any items. In XML markup language, you can create tags with any name. The most popular XML examples – sitemaps and RSS feeds.
XML file example:
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
In this example, the file contains breakfast_menu
Global tags, including food
categories, and each food
Categories include name
, price
, description
and calories
Label.
Now we start learning how to use XML and the Python Requests library. First we need to prepare the working environment.
Used to create new projects and virtual environment installations python3-virtualenv
pack. It requires separate requirements for each project. Installation in Debian/Ubuntu:
sudo apt install python3 python3-virtualenv -y
Create a project folder:
mkdir my_project
cd my_project
Create a Python virtual environment env
Name the folder:
python3 -m venv env
Start the virtual environment:
source env/bin/activate
Install dependencies for PIP:
pip3 install requests
Let’s start writing code.
create main.py
file and insert the following code:
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)
This code snippet helps us find all internal 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
Now we are writing the code to get the value from the inner element. Open main.py
document and replace the previous code with the following code:
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'))
We receive the 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
In the last step, we prettify the 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')))
Here is the 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
Source of material:
XML file example taken from W3 school.
Is my post useful? you can support me patreon.