BLOG

Working with XML in Python Requests library

Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. Wikipedia.

 

Requests is an elegant and simple HTTP library for Python, built for human beings.

Requests allows you to send HTTP/1.1 requests extremely easily. There’s no need to manually add query strings to your URLs, or to form-encode your POST data. Keep-alive and HTTP connection pooling are 100% automatic, thanks to urllib3. Official documentation.

 

 

# Preparing environment

 

Install OS dependencies:

sudo apt install python3 python3-virtualenv -y

 

Create project folder:

mkdir my_project
cd my_project

 

Create Python virtual environment with "env" named folder:

python3 -m venv env

 

Activate virtual environment:

source env/bin/activate

 

Install PIP dependencies:

pip3 install requests

 

 

# Writing code

 

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)

This code snippet help us to find all inner tags.

 

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 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'))

 

We received values:

(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

 

At the final step we may formatting output data like 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('Name: {}. Price: {}. Description: {}. Calories: {}'.format(item.findtext('name'), item.findtext('price'), item.findtext('description'), item.findtext('calories')))

 

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

Top button