-
Notifications
You must be signed in to change notification settings - Fork 0
/
corona.py
82 lines (70 loc) · 1.99 KB
/
corona.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
from bs4 import BeautifulSoup as bs
from urllib.request import urlopen as ur
import requests
import json
import pycountry
URL='https://www.worldometers.info/coronavirus/'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'}
page=requests.get(URL,headers=headers)
soup=bs(page.content,'html.parser')
table_body=soup.find('table')
rows = table_body.find_all('tr')
l=[]
d={
"Corona":[]
}
f={
"Main":[]
}
#to find the Main Header
ss=[]
mains=soup.findAll("div", {"id": "maincounter-wrap"} )
for i in mains:
ss.append(i.find("span").text)
temp=soup.find_all("div",{"class":"panel_flip"})
data1=[]
data2=[]
for k in temp:
x=k.findAll("div",{"class":"number-table-main"})
for i in x:
data1.append(i.text.strip())
m=k.findAll("span")
for j in m:
data2.append(j.text.strip())
#print(temp1)
cuinf,cloc=data1
mild,seri,dis,dea=data2
coc,cocd,rec=ss
f["Main"].append({
"CoronaCases":coc,
"CoronaCurrent":cuinf,
"CoronaClose":cloc,
"CoronaMild":mild,
"CoronaCritical":seri,
"CoronaDischarged":dis,
"CoronaDeaths":dea,
"CoronaDeaths":cocd,
"Recoverd":rec
})
#To get table data
#To get table data
mapping = {country.name: country.alpha_2 for country in pycountry.countries}
for row in rows:
cols=row.find_all('td')
z=['0' if v.text.strip() == "" else v.text.strip() for v in cols]
#print(z)
if len(z)!=0:
c,totc,newc,totd,newd,totrecv,Actcases,seri,avg,Avgd,*x=z
d['Corona'].append({
"Country":c,
"Code":str(mapping.get(c)).lower(),
"TotalCases":totc,
"NewCases":newc,
"TotalDeaths":totd,
"NewDeaths":newd,
"TotalRecoverd":totrecv,
"ActiveCases":Actcases,
"Serious":seri,
"Average":avg,
"AverageDeaths":Avgd
})