Q:

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

belongs to collection: Python miscellaneous programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python miscellaneous programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
English dictionary application using Python... >>
<< Greedy algorithm for a knapsack problem with the e...