How to extract Live Coronavirus data using APIs in Python?

Application Programming Interfaces

Submitting API Requests & Getting a Response

Web request-response complex

Step-by-Step Guide:

1. Getting a list of countries where coronavirus data exists

import requests
from datetime import date
todays_date = date.today()
base_url = "https://corona-api.com/countries"
get_countries = requests.get(url=base_url).json()['data']
[{'coordinates': {'latitude': 33, 'longitude': 65}, 'name': 'Afghanistan', 'code': 'AF', 'population': 29121286, 'updated_at': '2020-04-14T17:28:01.801Z', 'today': {'deaths': 2, 'confirmed': 49}, 'latest_data': {'deaths': 23, 'confirmed': 714, 'recovered': 40, 'critical': 0, 'calculated': {'death_rate': 3.221288515406162, 'recovery_rate': 5.602240896358544, 'recovered_vs_death_ratio': w'recovery_rate': 52.21052631578947, 'recovered_vs_death_ratio': None, 'cases_per_million_population': 165}}}, {'coordinates': {'latitude': 0, 'longitude': 0}, 'name': 'Åland Islands', 'code': 'AX', 'population': 26711, 'updated_at': '2020-04-14T17:28:01.801Z', 'today': {'deaths': 0, 'confirmed': 0}, 'latest_data': {'deaths': 0, 'confirmed': 0, 'recovered': 0, 'critical': 0, 'calculated': {'death_rate': None, 'recovery_rate': None, 'recovered_vs_death_ratio': None, 'cases_per_million_population': 0}}}, ...

2. Use the country code to search for a specific country and generate urls targets where requests will be sent

for country in get_countries:
country_codes = [country['code'] for country in get_countries]
for code in country_codes:
request_urls = [f"{base_url}/{code}" for code in country_codes]
country_codes = ['AF', 'AL', 'AX', 'AS', 'DZ', 'AD', 'AO', 'AI' ...]request_urls = ['https://corona-api.com/countries/AF', 'https://corona-api.com/countries/AL', ...]

3. Use the request_urls object to make the requests and receive a response

covid_19_data = []for url in request_urls:
country_data = requests.get(url=url).json()
covid_19_data.append(country_data)
covid_19_data = [{'data': {'coordinates': {'latitude': 33, 'longitude': 65}, 'name': 'Afghanistan', 'code': 'AF', 'population': 29121286, 'updated_at': '2020-04-15T10:21:11.506Z', 'today': {'deaths': 2, 'confirmed': 70}, 'latest_data': {'deaths': 25, 'confirmed': 784, 'recovered': 43, 'critical': 0, 'calculated': {'death_rate': 3.188775510204082, 'recovery_rate': 5.48469387755102, 'recovered_vs_death_ratio': None, 'cases_per_million_population': 20}}, 'timeline': [{'updated_at': '2020-04-15T10:18:59.805Z', 'date': '2020-04-15', 'deaths': 25, 'confirmed': 784, 'active': 716, 'recovered': 43, 'new_confirmed': 70, 'new_recovered': 3, 'new_deaths': 2, 'is_in_progress': True}, {'updated_at': '2020-04-14T21:33:12.000Z', 'date': '2020-04-14', 'deaths': 23, 'confirmed': 714, 'recovered': 40, 'new_confirmed': 49, 'new_recovered': 8, 'new_deaths': 2, 'active': 651}, ...}]}, '_cacheHit': True},Data collected for Afghanistan
Data collected for Albania
Data collected for American
...
Data collected for Yemen

4. Exporting the data to csv

from datetime import date
import pandas as pd
df = pd.json_normalize(covid_19_data)
todays_date = date.today()
file_name = f"{todays_date}_insert_file_name_here.csv"
file_save_destination = f"C:\\directory\\folder\\{file_name}"
df.to_csv(file_save_destination,
index=False,
header=True)

Concluding Remarks

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aaron Ginder

Aaron Ginder

A Google Cloud Data Engineering enthusiast