Corona Virus Live Updates for India (District Wise) – Using Python

Here, we will see how we can create a District wise coronavirus tracker with the help of python?
Submitted by Abhinav Gangrade, on June 19, 2020

Module used:

In this script, we will use requests module to get the data from the API that is in the JSON format and we will use the pprint module to make the data readable.

What is API?

API (Application Programming Interface) is used to create software. For example: If you wanted to make a weather analysis app or web app, you will use any weather-related API for getting the details.

The API we will use is: https://api.covid19india.org/state_district_wise.json

The above-mentioned URL will provide us the data in the form of Dictionary(as JSON is always in the form of Dictionary).

requests Module:

requests is a python module that will allow us to send the HTTP requests to get the data from a particular URL. For example, if we want the data from the above-mentioned URL, then we will send the HTTP request to the site to get the data with the help of requests module.

pprint Module:

pprint module helps us to make our data easily readable.

How we can Download pprint and requests?

General way:

    requests => pip install requests
    pprint=> pip install pprint

Pycharm users: Go to the project interpreter and install these modules.

Program:

# importing the modules
import requests, pprint

# setting the url
url="https://api.covid19india.org/state_district_wise.json"

# send the http request
# Store the data in variable
State_data=requests.get(url)

# Now print the data
print(State_data)

Output:

<Response [200]>

Now the response is 200 this means that our request is accepted and we can access the data.

As now the data is in the form of JSON so we will load that let's see the code,

# importing the modules
import requests,pprint

# setting the url
url="https://api.covid19india.org/state_district_wise.json"

# send the http request
# Store the data in variable
State_data=requests.get(url)

# Now print the data
print(State_data)

# Now load the json data with the help
# of response we get from the url
Json_state_data=State_data.json()

# with the help of pprint print the data
pprint.pprint(Json_state_data)

Output:

Output

Now you can see the state-wise data.

Now let's see the code for the district (In my case the district is Indore (Madhya Pradesh)).

# importing the modules
import requests,pprint

# setting the url
url="https://api.covid19india.org/state_district_wise.json"

# send the http request
# Store the data in variable
State_data=requests.get(url)

# Now print the data
print(State_data)

# Now load the json data with the help
# of response we get from the url
Json_state_data=State_data.json()

# print the district wise data
# as the data is in the form of dictionary
# The key value pair is like 'districtData'
# inside that the district name
Indore_data=Json_state_data['Madhya Pradesh']['districtData']['Indore']
pprint.pprint(Indore_data)

Output:

<Response [200]>
{'active': 908,
 'confirmed': 4246,
 'deceased': 189,
 'delta': {'confirmed': 55, 'deceased': 4, 'recovered': 18},
 'notes': '',
 'recovered': 3149}

Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.