Reading data from API#

Using the Countries REST API

Importing libraries and packages#

 1# Mathematical operations and data manipulation
 2import pandas as pd
 3
 4# Data gathering
 5import urllib.request
 6import urllib.parse
 7from urllib.error import HTTPError, URLError
 8
 9# Read data
10import json

Set paths#

1# Path to datasets directory
2data_path = "./datasets"
3# Path to assets directory (for saving results to)
4assets_path = "./assets"

Loading data#

 1def get_country_data(country):
 2    """
 3    Function to get data about a country from "https://restcountries.com" API
 4    """
 5    serviceurl = "https://restcountries.com/v3.1/name/"
 6    country_name = str(country)
 7    url = serviceurl + country_name
 8
 9    try:
10        uh = urllib.request.urlopen(url)
11    except HTTPError:
12        data = "Sorry! Could not retrieve anything on {}".format(country_name)
13        return data
14    except URLError as e:
15        data = "Failed to reach a server. Reason: {}".format(e.reason)
16        return data
17    else:
18        data = uh.read().decode()
19        print(
20            "Retrieved data on {}. Total {} characters read.".format(
21                country_name, len(data)
22            )
23        )
24        return data
1name_country = "peru"
2retrieved_data = get_country_data(name_country)
Retrieved data on peru. Total 2763 characters read.
1x = json.loads(retrieved_data)
2# Load the only element
3y = x[0]
4type(y)
dict
1y.keys()
dict_keys(['name', 'tld', 'cca2', 'ccn3', 'cca3', 'cioc', 'independent', 'status', 'unMember', 'currencies', 'idd', 'capital', 'altSpellings', 'region', 'subregion', 'languages', 'translations', 'latlng', 'landlocked', 'borders', 'area', 'demonyms', 'flag', 'maps', 'population', 'gini', 'fifa', 'car', 'timezones', 'continents', 'flags', 'coatOfArms', 'startOfWeek', 'capitalInfo', 'postalCode'])
1for k, v in y.items():
2    print("{}: {}".format(k, v))
name: {'common': 'Peru', 'official': 'Republic of Peru', 'nativeName': {'aym': {'official': 'Piruw Suyu', 'common': 'Piruw'}, 'que': {'official': 'Piruw Ripuwlika', 'common': 'Piruw'}, 'spa': {'official': 'República del Perú', 'common': 'Perú'}}}
tld: ['.pe']
cca2: PE
ccn3: 604
cca3: PER
cioc: PER
independent: True
status: officially-assigned
unMember: True
currencies: {'PEN': {'name': 'Peruvian sol', 'symbol': 'S/ '}}
idd: {'root': '+5', 'suffixes': ['1']}
capital: ['Lima']
altSpellings: ['PE', 'Republic of Peru', 'República del Perú']
region: Americas
subregion: South America
languages: {'aym': 'Aymara', 'que': 'Quechua', 'spa': 'Spanish'}
translations: {'ara': {'official': 'جمهورية بيرو', 'common': 'بيرو'}, 'bre': {'official': 'Republik Perou', 'common': 'Perou'}, 'ces': {'official': 'Peruánská republika', 'common': 'Peru'}, 'cym': {'official': 'Republic of Peru', 'common': 'Peru'}, 'deu': {'official': 'Republik Peru', 'common': 'Peru'}, 'est': {'official': 'Peruu Vabariik', 'common': 'Peruu'}, 'fin': {'official': 'Perun tasavalta', 'common': 'Peru'}, 'fra': {'official': 'République du Pérou', 'common': 'Pérou'}, 'hrv': {'official': 'Republika Peru', 'common': 'Peru'}, 'hun': {'official': 'Perui Köztársaság', 'common': 'Peru'}, 'ita': {'official': 'Repubblica del Perù', 'common': 'Perù'}, 'jpn': {'official': 'ペルー共和国', 'common': 'ペルー'}, 'kor': {'official': '페루 공화국', 'common': '페루'}, 'nld': {'official': 'Republiek Peru', 'common': 'Peru'}, 'per': {'official': 'جمهوری پرو', 'common': 'پرو'}, 'pol': {'official': 'Republika Peru', 'common': 'Peru'}, 'por': {'official': 'República do Peru', 'common': 'Perú'}, 'rus': {'official': 'Республика Перу', 'common': 'Перу'}, 'slk': {'official': 'Peruánska republika', 'common': 'Peru'}, 'spa': {'official': 'República de Perú', 'common': 'Perú'}, 'swe': {'official': 'Republiken Peru', 'common': 'Peru'}, 'tur': {'official': 'Peru Cumhuriyeti', 'common': 'Peru'}, 'urd': {'official': 'جمہوریہ پیرو', 'common': 'پیرو'}, 'zho': {'official': '秘鲁共和国', 'common': '秘鲁'}}
latlng: [-10.0, -76.0]
landlocked: False
borders: ['BOL', 'BRA', 'CHL', 'COL', 'ECU']
area: 1285216.0
demonyms: {'eng': {'f': 'Peruvian', 'm': 'Peruvian'}, 'fra': {'f': 'Péruvienne', 'm': 'Péruvien'}}
flag: 🇵🇪
maps: {'googleMaps': 'https://goo.gl/maps/uDWEUaXNcZTng1fP6', 'openStreetMaps': 'https://www.openstreetmap.org/relation/288247'}
population: 32971846
gini: {'2019': 41.5}
fifa: PER
car: {'signs': ['PE'], 'side': 'right'}
timezones: ['UTC-05:00']
continents: ['South America']
flags: {'png': 'https://flagcdn.com/w320/pe.png', 'svg': 'https://flagcdn.com/pe.svg'}
coatOfArms: {'png': 'https://mainfacts.com/media/images/coats_of_arms/pe.png', 'svg': 'https://mainfacts.com/media/images/coats_of_arms/pe.svg'}
startOfWeek: monday
capitalInfo: {'latlng': [-12.05, -77.05]}
postalCode: {'format': '#####', 'regex': '^(\\d{5})$'}
 1def build_country_database(list_country):
 2    """
 3    Takes a list of country names.
 4    Output a DataFrame with key information about those countries.
 5    """
 6    # Define an empty dictionary with keys
 7    country_dict = {
 8        "Country": [],
 9        "Capital": [],
10        "Region": [],
11        "Sub-region": [],
12        "Population": [],
13        "Latitude": [],
14        "Longitude": [],
15        "Area": [],
16    }
17
18    for c in list_country:
19        data = get_country_data(c)
20        if data != 0:
21            x = json.loads(data)
22            y = x[0]
23            country_dict["Country"].append(y["name"])
24            country_dict["Capital"].append(y["capital"])
25            country_dict["Region"].append(y["region"])
26            country_dict["Sub-region"].append(y["subregion"])
27            country_dict["Population"].append(y["population"])
28            country_dict["Latitude"].append(y["latlng"][0])
29            country_dict["Longitude"].append(y["latlng"][1])
30            country_dict["Area"].append(y["area"])
31
32    # Return as a Pandas DataFrame
33    return pd.DataFrame(country_dict)
34
35
36df = build_country_database(
37    ["Niger", "Switzerland", "France", "Russia", "Kenya", "Singapore"]
38)
Retrieved data on Niger. Total 5601 characters read.
Retrieved data on Switzerland. Total 3053 characters read.
Retrieved data on France. Total 2911 characters read.
Retrieved data on Russia. Total 2900 characters read.
Retrieved data on Kenya. Total 2719 characters read.
Retrieved data on Singapore. Total 2977 characters read.
1df
Country Capital Region Sub-region Population Latitude Longitude Area
0 {'common': 'Niger', 'official': 'Republic of N... [Niamey] Africa Western Africa 24206636 16.000000 8.0 1267000.0
1 {'common': 'Switzerland', 'official': 'Swiss C... [Bern] Europe Western Europe 8654622 47.000000 8.0 41284.0
2 {'common': 'France', 'official': 'French Repub... [Paris] Europe Western Europe 67391582 46.000000 2.0 551695.0
3 {'common': 'Russia', 'official': 'Russian Fede... [Moscow] Europe Eastern Europe 144104080 60.000000 100.0 17098242.0
4 {'common': 'Kenya', 'official': 'Republic of K... [Nairobi] Africa Eastern Africa 53771300 1.000000 38.0 580367.0
5 {'common': 'Singapore', 'official': 'Republic ... [Singapore] Asia South-Eastern Asia 5685807 1.366667 103.8 710.0