Getting data from an API

Once an API is running, we may start a client. This client fires a call. This call is fired with “requests.get”. Between the brackets, we see a call that could be run from any webbrowser.

The reponse can then be received with response.json()

The json is very similar to a dataframe. The dataframe can then be set up by reading a row in the json and appending the row to the dataframe.

import requests
import pandas as pd
doel = []
df = pd.DataFrame(doel, columns=["Author", "FirstLine", "Index", "Year", "Title"])
response = requests.get("http://127.0.0.1:5000/api/v1/resources/books/all")
if (response.status_code == 200):
  print('Status code: ',response.status_code)
  data = response.json()
  for i in range(len(data)):
    rij = data[i]
    df = df.append([[]])
    df.iloc[i] = rij
elif (response.status_code == 404) :
  print("Result not found!")
  print('Nmbr rows: ',df.shape[0],' Nmbr columns: ',df.shape[1])

Door tom