Skip to content

Instantly share code, notes, and snippets.

@ksv-muralidhar
Created February 16, 2021 07:11
Show Gist options
  • Save ksv-muralidhar/c771d495aeaa266fcb1542f7473de505 to your computer and use it in GitHub Desktop.
Save ksv-muralidhar/c771d495aeaa266fcb1542f7473de505 to your computer and use it in GitHub Desktop.
API data extraction
import requests
import pandas as pd
from IPython.display import display
def api_extract(country,n=-1):
'''
Function to extract university information from an API "http://universities.hipolabs.com/search?country="
param country: Country for which one needs the university data
param n: Number of records to return. -1 returns all the data
'''
err=0 # initializing an error variable to 0
##################################
# API REQUEST #
##################################
req = requests.get(f"http://universities.hipolabs.com/search?country={country.lower().replace(' ','%20')}") # API request, converting 'country' to lowercase and replacing spaces with %20 and sending the API request
if req.status_code!= 200: # if status code != 200 error variable is assigned to 1
err = 1
js = req.json() # Getting the request output in JSON format
try:
if err==1:
raise Exception() # raises an exception in case of an error
df = pd.DataFrame()
##################################
# DATA STRUCTURING #
##################################
for i in js[0].keys(): # Structuring the JSON output into a data frame
df[i] = [j[i] for j in js]
except:
return "No Data Found" # Returning no data upon an error
else:
if n!=-1:
return df.head(n) #returns top n records
else:
return df # returns the whole data frame if n=-1
# API request for university data in the US
api_extract("United States",10)
##################################
# API REQUEST #
##################################
req = requests.get("https://api.covid19api.com/summary") # sending request to API
# req.status_code #Need not check the status code since there is no scope for error unless the API in not functional
js = req.json() # extracting the API result inJSON format
country_data = pd.DataFrame()
##################################
# DATA STRUCTURING #
##################################
for i in range(len(js["Countries"])):
js["Countries"][i].pop("Premium")
country_data = country_data.append(pd.DataFrame(js["Countries"][i],index=[i])).copy()
country_data_main = country_data.copy()
display(country_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment