-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJohnsHopkinsScrapingCovid19.py
179 lines (116 loc) · 5.25 KB
/
JohnsHopkinsScrapingCovid19.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from datetime import date
import json
import time
import os
import re
import requests
# Chrome preference
chrome_prefs = {}
options = Options()
options.headless = True
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('disable-infobars')
options.add_argument("--disable-extensions")
options.experimental_options["prefs"] = chrome_prefs
# Start webdriver -- headless mode
driver = webdriver.Chrome(executable_path="./drivers/chromedriver", options=options)
# Url
base_url = 'https://www.arcgis.com/apps/opsdashboard/index.html#/bda7594740fd40299423467b48e9ecf6'
waitTime = 50
def wait(value = 3):
time.sleep(value)
def loadTotalConfirmed():
try:
element = WebDriverWait(driver, waitTime).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='ember27']//div[@class='flex-vertical flex-fix allow-shrink']"))).text
return element.split('\n')[1].strip().replace(",", ".")
except Exception as error:
raise Exception("[TOTAL_CONFIRMED_CASES] - Error:%s" % error)
def loadConfirmedCasesByCountry():
try:
element = WebDriverWait(driver, waitTime).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='ember34']//nav[@class='feature-list']")))
wait(10)
data = element.find_elements_by_tag_name("h5")
confirmedCases = []
for country in data:
casesInformation = country.text.split(' ')
confirmedCases.append({
'country': casesInformation[1].strip(),
'confirmed': casesInformation[0].strip().replace(",", ".")
})
return confirmedCases
except Exception as error:
raise Exception("[CONFIRMED_CASES_BY_COUNTRY] - Error:%s" % error)
def loadGlobalDeathsByCountry():
try:
element = WebDriverWait(driver, waitTime).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='ember111']//nav[@class='feature-list']")))
wait(10)
data = element.find_elements_by_xpath("//div[@id='ember111']//nav[@class='feature-list']//div[contains(@class,'flex-fluid list-item-content overflow-hidden')]")
deathsCases = []
for country in data:
casesInformation = country.text.replace("deaths", "").split('\n')
deathsCases.append({
'country': casesInformation[1].strip(),
'deaths': casesInformation[0].strip().replace(",", ".")
})
return deathsCases
except Exception as error:
raise Exception("[GLOBAL_DEATHS_BY_COUNTRY] - Error:%s" % error)
def loadTotalRecovered():
try:
element = WebDriverWait(driver, waitTime).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='ember118']//div[@class='flex-vertical flex-fix allow-shrink']"))).text
return element.split('\n')[1].strip().replace(",", ".")
except Exception as error:
raise Exception("[TOTAL_RECOVERED_CASES] - Error:%s" % error)
def loadGlobalRecoveredByCountry():
try:
WebDriverWait(driver, waitTime).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='ember240']//div[3]//a[1]"))).click()
wait(5)
element = WebDriverWait(driver, waitTime).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='ember125']//nav[@class='feature-list']")))
wait(5)
data = element.find_elements_by_xpath("//div[@id='ember125']//nav[@class='feature-list']//div[contains(@class,'flex-fluid list-item-content overflow-hidden')]")
recoveredCases = []
for country in data:
casesInformation = country.text.replace("recovered", "").split('\n')
recoveredCases.append({
'country': casesInformation[1].strip(),
'recovered': casesInformation[0].strip().replace(",", ".")
})
return recoveredCases
except Exception as error:
raise Exception("[GLOBAL_RECOVERED_BY_COUNTRY] - Error:%s" % error)
def main():
try:
print('Loading')
driver.get(base_url)
totalConfirmed = loadTotalConfirmed()
confirmedCasesByCountry = loadConfirmedCasesByCountry()
globalDeathsByCountry = loadGlobalDeathsByCountry()
recoveredByCountry = loadGlobalRecoveredByCountry()
totalRecovered = loadTotalRecovered()
result = {
'total_confirmed': totalConfirmed,
'total_recovered': totalRecovered,
'confirmed_by_country': confirmedCasesByCountry,
'global_deaths_by_country': globalDeathsByCountry,
'recovered_by_country': recoveredByCountry
}
print(result)
except Exception as error:
raise Exception(error)
except KeyboardInterrupt as error:
raise Exception("Process has been cancelled by user")
if __name__ == '__main__':
try:
main()
except Exception as error:
print(error)
finally:
driver.quit()