I hope you all are safe and sound and at home. Do not step out of your house unless it’s absolutely necessary with proper precautions. Coronavirus cases are increasing day by day. The second wave has hit very badly in many areas. It’s very important to get vaccinated. But, getting a slot seems next to impossible. Even I am not able to get my hands on one. So I was trying to think of a better idea possible and I tried to automate the availability of the slot.
What will be covered in this Blog?
- What is Web Scraping?
- The Web-Scaping Process and its Uses.
- What is Coronavirus and CoWin?
- How to Automate vaccine slots availability
What is Web Scrapping?
- Always be respectful and try to get permission to scrape, do not bombard a website with scraping requests, otherwise, your IP address may get blocked!
- Be aware that websites change often, meaning your code could go from working to totally broken from one day to the next.
The process:
- Request for a response from the webpage.
- Parse and extract with the help of Beautiful soup and lxml.
- Download and export the data with pandas into excel
Uses:
If you wish to know about it further. I am attaching the Wikipedia link here. You can have a look.
What is Coronavirus?
The COVID-19 virus spreads primarily through droplets of saliva or discharge from the nose when an infected person coughs or sneezes
If you wish to know about it further. I am attaching the Wikipedia link here. You can have a look.
What is CoWin?
The data source
Module Used:
Use the requests library to grab the page. This may fail if you have a firewall blocking Python. Sometimes you need to run this twice if it fails the first time.
Time to Code!
pip install requests
import requests
from datetime import datetime, timedelta
import time
import json
Let's display a welcome message. we will make use of
`print`
method for the same.print("Starting search for Covid vaccine slots!")
age = 52
pinCodes = ["462003"]
print_flag = 'Y'
num_days = 2
- The Slots are available in two categories,
andAge 45+
. Let's proceed with Age 45+ for now, you can set it according to your need.Age 18+
- You can perform the search based on
orPincode
. Since we are looking for the slots available nearby, I am making use of Pin-code. You can also pass multiple pin-codes, separated byDistrict
in the list.commas
- We only want to check for slots availability for the next two days for let's give
asnum_days
here.2
- Lastly, set the flag value to
.y
datetime
method to calculate today's date.actual = datetime.today()
#output:
2021-05-13 13:35:24.396612
timedelta
method to convert it into list formatlist_format = [actual + timedelta(days=i) for i in range(num_days)]
#Output:
[datetime.datetime(2021, 5, 13, 13, 39, 18, 496330), datetime.datetime(2021, 5, 14, 13, 39, 18, 496330)]
strftime
method to do so. Note that we are storing date in date-month-year
format.actual_dates = [i.strftime("%d-%m-%Y") for i in list_format]
#Output:
['13-05-2021', '14-05-2021']
Let's introduce two more loops here:
- one in order to fetch details for each pin-code.
- second in order to fetch details for each date in the given pin-code.
URL = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode={}&date={}".format(pinCode, given_date)
pincode
and date
. Every time the inner loop runs, this URL will be called and the respective date and pin-code will be passed as arguments for each case.header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'}
requests
and we will make use of get
method.result = requests.get( URL, headers=header)
text
method for the same.print(result.text)
#OUTPUT:
{"centers":[{"center_id":691508,"name":"Navin Girls HSS Tulsi Nagar 18","address":"1250 Tulsi Nagar HOSPITAL","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"b63ce111-23dd-4d4e-9f1c-c97073c57a52","date":"13-05-2021","available_capacity":0,"min_age_limit":18,"vaccine":"COVAXIN","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":570554,"name":"SAHTI Vaishali Kotra
KNK","address":"Govt. Higher Sec. Kamla Nehru Girls School Teenshed TT Nagar","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"93144388-6dca-4f7c-9aba-ecd5b120fa94","date":"13-05-2021","available_capacity":215,"min_age_limit":45,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":592476,"name":"25 Battalion Campus BhadBhada","address":"25th Battalion Bhadbhada Bhopal","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"b857cd10-b46f-404f-a5de-81032b667e46","date":"13-05-2021","available_capacity":0,"min_age_limit":45,"vaccine":"COVAXIN","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":570591,"name":"GHMC- Ward 29 Office 18","address":"Pandit Khushilal Sharma Parisar","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"5bcd92b4-a476-4c47-a203-3e96179e5beb","date":"13-05-2021","available_capacity":0,"min_age_limit":18,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":609392,"name":"Govt School Nayabasera 18","address":"NAYABASERA SANJEEVANI","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"60306cc5-15a4-4195-876a-2889676631c0","date":"13-05-2021","available_capacity":0,"min_age_limit":18,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":561924,"name":"Saraswati Shisu Mandir Sch Cov","address":"Saraswati Shishu Mandir School Shivaji Nagar Bhopal Madhya Pradesh India","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"e1f4f7a1-42d5-4f58-934b-ed031fc3e7c0","date":"13-05-2021","available_capacity":137,"min_age_limit":45,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":591027,"name":"CD RAJ BHAVAN CVX","address":"RAJ BHAVAN BHOPAL","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"52de668d-5526-4e09-8c41-f34c1912302d","date":"13-05-2021","available_capacity":0,"min_age_limit":45,"vaccine":"COVAXIN","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":667429,"name":"CD Vallabh Bhawan CVX","address":"Vallabh Bhawan Bhopal","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"e8202c2d-b545-401c-b266-8bf9102687e2","date":"13-05-2021","available_capacity":0,"min_age_limit":45,"vaccine":"COVAXIN","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":561310,"name":"Govt Kanya School Nehru Nagar","address":"Govt Kanya School Nehru Nagar","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"85a85c27-cabd-4058-b902-7b0bf457cb44","date":"13-05-2021","available_capacity":114,"min_age_limit":45,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":636442,"name":"CD VIDHAN SABHA COVISHIELD","address":"VIDHAN SABHA","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"6010b80f-a818-4cf5-ad67-90a0cb49f1d6","date":"13-05-2021","available_capacity":0,"min_age_limit":45,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":611640,"name":"Kopal HSS Nehru Nagar18","address":"Kopal HSS Nehru Nagar Bhopal","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"b4a74e1f-cc5b-498a-b92d-25bf5c3bc072","date":"13-05-2021","available_capacity":0,"min_age_limit":18,"vaccine":"COVAXIN","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":639426,"name":"BANGANG WARD 25 CVX","address":"SANJEEVNI BANGANGA","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"def621bf-2e3e-4514-a605-9debd696d8ef","date":"13-05-2021","available_capacity":0,"min_age_limit":45,"vaccine":"COVAXIN","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":636455,"name":"JAHANGIRABAD SANJEEVANI","address":"JAHANGIRABAD","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"18:00:00","fee_type":"Free","sessions":[{"session_id":"a58f02b7-a252-4daf-a541-b7ddf9bae21c","date":"19-05-2021","available_capacity":0,"min_age_limit":45,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-06:00PM"]}]},{"center_id":627202,"name":"CD Sewaniya Gaud","address":"CD Sewaniya Gaud","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"18:00:00","fee_type":"Free","sessions":[{"session_id":"80130fd2-483a-4bb7-876a-9f2130484994","date":"19-05-2021","available_capacity":0,"min_age_limit":45,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-06:00PM"]}]}]}
json
here. Let's cast the data into json
file.if result.ok:
response_json = result.json()
flag
as false
.flag = False
if response_json["centers"]:
if(print_flag.lower() =='y'):
for center in response_json["centers"]:
print(center)
#OUTPUT:
{'center_id': 691508, 'name': 'Navin Girls HSS Tulsi Nagar 18', 'address': '1250 Tulsi Nagar HOSPITAL', 'state_name': 'Madhya Pradesh', 'district_name': 'Bhopal', 'block_name': 'PHANDA', 'pincode': 462003, 'lat': 23, 'long': 77, 'from': '09:00:00', 'to': '17:00:00', 'fee_type': 'Free', 'sessions': [{'session_id': 'b63ce111-23dd-4d4e-9f1c-c97073c57a52', 'date': '13-05-2021', 'available_capacity': 0, 'min_age_limit': 18, 'vaccine': 'COVAXIN', 'slots': ['09:00AM-11:00AM', '11:00AM-01:00PM', '01:00PM-03:00PM', '03:00PM-05:00PM']}]}
for session in center["sessions"]:
if (session["min_age_limit"] <= age and session["available_capacity"] > 0 ):
print('Pincode: ' + pinCode)
print("Available on: {}".format(given_date))
print("\t", center["name"])
print("\t", center["block_name"])
print("\t Price: ", center["fee_type"])
print("\t Availablity : ", session["available_capacity"])
if(session["vaccine"] != ''):
print("\t Vaccine type: ", session["vaccine"])
print("\n")
counter += 1
if(counter == 0):
print("No Vaccination slot avaliable!")
else:
print("Search Completed!")
dt = datetime.now() + timedelta(minutes=3)
while datetime.now() < dt:
time.sleep(1)
You should definitely check out my other Blogs:
Resources:
- https://apisetu.gov.in/public/marketplace/api/cowin#/Appointment%20Availability%20APIs/calendarByPin
- cowin.gov.in/home
- pypi.org/project/requests