-
Notifications
You must be signed in to change notification settings - Fork 0
/
coronavirus_stats.py
63 lines (51 loc) · 1.64 KB
/
coronavirus_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import requests
from bs4 import BeautifulSoup
import os
import numpy as np
import matplotlib.pyplot as plt
extract_contents = lambda row: [x.text.replace('\n', '') for x in row]
URL = 'https://www.mohfw.gov.in/'
heading = ['SNo', 'State','Indian-Confirmed',
'Foreign-Confirmed','Cured','Death']
response = requests.get(URL).content
soup = BeautifulSoup(response, 'html.parser')
header = extract_contents(soup.tr.find_all('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) == 5:
# last row
stat = ['', *stat]
stats.append(stat)
elif len(stat) == 6:
stats.append(stat)
stats[-1][1] = "Total Cases"
stats.remove(stats[-1])
objects = []
for row in stats :
objects.append(row[1])
y_pos = np.arange(len(objects))
performance = []
for row in stats :
performance.append(int(row[2]) + int(row[3]))
table = stats
def print_table(table):
longest_cols = [
(max([len(str(row[i])) for row in table]) + 3)
for i in range(len(table[0]))
]
row_format = "".join(["{:>" + str(longest_col) + "}" for longest_col in longest_cols])
for row in table:
print(row_format.format(*row))
stats.insert(0,heading)
print_table(table)
plt.barh(y_pos, performance, align='center', alpha=0.5,
color=(240/256.0, 128/256.0, 128/256.0),
edgecolor=(0/256.0, 0/256.0, 0/256.0))
plt.yticks(y_pos, objects)
plt.xlim(1,150)
plt.xlabel('Number of Cases')
plt.title('Corona Virus Cases')
plt.show()