Skip to content
Related Articles
Open in App
Not now

Related Articles

Corona Virus Live Updates for India – Using Python

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 06 Oct, 2020
Improve Article
Save Article

As we know the whole world is being affected by the COVID-19 pandemic and almost everyone is working from home. We all should utilize this duration at best, to improve our technical skills or writing some good Pythonic scripts. 
Let’s see a simple Python script to demonstrate the state-wise coronavirus cases in India. This Python script fetches the live data from the Ministry of Health Affairs Official Website. Then data is represented in the horizontal bar graph.
To run this script follow the below installation – 

$ pip install bs4
$ pip install tabulate
$ pip install matplotlib
$ pip install numpy 
$ pip install requests

Let’s try to execute the script step-by-step. 
Step #1: 


# importing libraries
import requests
from bs4 import BeautifulSoup
from tabulate import tabulate
import os
import numpy as np
import matplotlib.pyplot as plt

Step #2: 


extract_contents = lambda row: [x.text.replace('\n', '') for x in row]
SHORT_HEADERS = ['SNo', 'State','Indian-Confirmed(Including Foreign Confirmed)','Cured','Death']
response = requests.get(URL).content
soup = BeautifulSoup(response, 'html.parser')
header = extract_contents('th'))
stats = []
all_rows = soup.find_all('tr')
for row in all_rows:
    stat = extract_contents(row.find_all('td'))
    if stat:
        if len(stat) == 4:
            # last row
            stat = ['', *stat]
        elif len(stat) == 5:
stats[-1][0] = len(stats)
stats[-1][1] = "Total Cases" 

Step #3: 


objects = []
for row in stats :
y_pos = np.arange(len(objects))
performance = []
for row in stats[:len(stats)-1] :
table = tabulate(stats, headers=SHORT_HEADERS)


Step #4: 


plt.barh(y_pos, performance, align='center', alpha=0.5,
                 color=(234/256.0, 128/256.0, 252/256.0),
                 edgecolor=(106/256.0, 27/256.0, 154/256.0))
plt.yticks(y_pos, objects)
plt.xlabel('Number of Cases')
plt.title('Corona Virus Cases')



My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!