How to extract data from API (Co-win Public API) using Python?

May 16, 2021, 8:55 p.m.


In basic terms, API just allow applications to communicate with one another.
Web based API is an extensible framework for building HTTP based services that can be accessed in different applications on different platforms such as web, windows, mobile etc.

In basic terms, API just allow applications to communicate with one another.
Web based API is an extensible framework for building HTTP based services that can be accessed in different applications on different platforms such as web, windows, mobile etc.

Http specifies how requests and response are to be formatted and transmitted.

Get Requests:

1) Obtain data from server.

2) Can be bookmarked.

3) Parameters are added directly to the URL.

4) Not used for sensitive information.

 

Post Requests:

1) Alter state or to send confidential information.

2) Parameters are added in a separate body.

Step1: Import required Packages

import requests
import json
import pandas as pd

Step 2: Base url to get slots by passing the value to pincode and date

base_url="https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=110053&date=21-05-2021"

Step 3: Go to link to get User-Agent details of your browser User Agent Details

headers={
 "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/90.0.4430.212 Safari/537.36"
}

Step 4: Check the status

response=requests.get(base_url,headers=headers)
response.status_code

Step 5: It will work only if some data is available for key sessions

data=response.json()
if data['sessions']:
 session=pd.DataFrame(data['sessions'])
 j=pd.DataFrame(session[['min_age_limit','name','address','vaccine','available_capacity','fee_type']])

Step 6: Rename the column to make them more readable

j.rename(columns={'min_age_limit':'Minimum Age','name':'Name','address':'Address','vaccine':'Vaccine','available_capacity':'Slots','fee_type':'Free/Paid'},inplace=True)
j




Tags


Downloads

Download Notebook

Comments