-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebscraper.py
92 lines (77 loc) · 2.31 KB
/
webscraper.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from bs4 import BeautifulSoup,Comment
import requests
import json
import jsons as j
import matplotlib.pyplot as plt
import operator
import numpy
import html
def web():
def absolute_value(val):
a = numpy.round(val/100.*numpy.array(top10).sum(), 0)
return int(a)
def absolute_values(val):
a = numpy.round(val/100.*numpy.array(least10).sum(), 0)
return int(a)
list=[]
response=requests.get("https://covidindia.org/").content
soup = BeautifulSoup(response, 'html5lib')
table=soup.findAll('table')[0].findAll('tr')
header=table[0].find_all('th')
head=[]
for i in header:
head.append(i.text)
list.append(head)
for rows in table[1:]:
u=[]
tds=rows.find_all('td')
for i in tds:
u.append(i.text)
list.append(u)
past=j.load()
print(list)
curr={x[0]:x[1:] for x in list[1:-1]}
print(curr)
j.save(curr)
changed=False
for states in curr:
if states not in past:
changed=True
past[states]=['0','0','0','0']
states=[x for x in curr.keys()]
states=states[:-1]
print(states)
data_total=[int(curr[x][0]) for x in states]
data_cure=[int(curr[x][1]) for x in states]
data_death=[int(curr[x][2]) for x in states]
b={}
for i in range(0,len(states)):
b[states[i]]=data_total[i]
sorted_curr=dict(sorted(b.items(),key=operator.itemgetter(1),reverse=True))
states=[x for x in sorted_curr.keys()]
data_total=[int(curr[x][0]) for x in states]
top10=data_total[0:10]
least10=data_total[-10:]
plt.clf()
plt.pie(top10,labels=states[0:10],startangle=90,autopct=absolute_value,shadow=True)
plt.axis('equal')
plt.title("Total cases in Top 10 states in india\n")
print(sorted_curr)
plt.savefig("static/images/a.png")
plt.clf()
plt.clf()
plt.pie(least10,labels=states[-10:],startangle=45,autopct=absolute_values,shadow=True)
plt.title("Total cases in Least 10 states in india")
plt.savefig("static/images/b.png")
if changed:
print("changed")
else:
print("not changed")
total=sum(data_total)
death=sum(data_death)
cure=sum(data_cure)
print(len(data_total))
li=[total,cure,death]
print(li)
return li
d=web()