-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovid19.py
72 lines (58 loc) · 1.87 KB
/
covid19.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
import json
import requests
import lxml.html as lh
import pandas as pd
from htmlmin import minify
# ///////////////////////////////////////////////////
# scrape url table and convert to panda datatable
# ///////////////////////////////////////////////////
url = 'https://www.worldometers.info/coronavirus/country/us/'
page = requests.get(url)
doc = lh.fromstring(page.content)
tr_elements = doc.xpath('//tr')
col = []
i = 0
if not tr_elements:
print("No row elements at " + url)
else:
for header in tr_elements[0]:
i+=1
name = header.text_content()
col.append((name, []))
for j in range(1, 55):
tr_element = tr_elements[j]
i = 0
for row in tr_element.iterchildren():
data = row.text_content()
min_data = minify(data)
value = min_data.replace(',', '')
if i > 0:
try:
data = int(value)
except:
pass
col[i][1].append(value)
i+=1
d = {title:column for (title,column) in col}
df = pd.DataFrame(d)
# ///////////////////////////////////////////////////
# update states.geojson with covid data from datatable
# ///////////////////////////////////////////////////
with open('./source/gallery/data/states.geojson') as f:
geojson = json.load(f)
for i in df.index:
if i < 52:
for feature in geojson['features']:
stateName = feature['properties']['NAME']
dataStateName = str.strip(df['USAState'][i])
if (stateName == dataStateName):
feature['properties']['COVID_CONFIRMED'] = df['TotalCases'][i]
feature['properties']['COVID_NEW'] = df['NewCases'][i]
feature['properties']['COVID_DEATHS'] = df['TotalDeaths'][i]
# print out results
print(df['USAState'][i], df['TotalCases'][i], df['NewCases'][i], df['TotalDeaths'][i])
with open('./source/gallery/data/states.geojson', 'w') as f:
f.seek(0)
json.dump(geojson, f, indent=2)
f.truncate()
f.close