-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnndss_scrape.py
executable file
·224 lines (175 loc) · 6.87 KB
/
nndss_scrape.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#! /usr/bin/python3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import datetime
from bs4 import BeautifulSoup
import csv
import os
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',options=options)
col_headings = ['YEAR', 'ACT', 'NSW', 'NT', 'QLD', 'SA', 'TAS', 'VIC', 'WA', 'Aust', 'condition', 'updated']
with open("/home/pi/nndss/data/count_jur.csv", "w") as f:
wr = csv.writer(f)
wr.writerow(col_headings)
driver.get("http://www9.health.gov.au/cda/source/rpt_4_sel.cfm")
soup = BeautifulSoup(driver.page_source, 'html.parser')
causeList = {}
table = soup.find('select')
rows = table.find_all('option')
for tag in rows:
idNo = tag.get('value')
cause = tag.text.strip()
causeList[idNo] = cause
for key in causeList:
report = driver.find_element_by_id("REPORT_OPTION_1")
report.click()
elem = driver.find_element_by_name("CAUSE")
elem.send_keys(causeList[key])
button = driver.find_element_by_name("CTIME1")
button.click()
time.sleep(3)
soup = BeautifulSoup(driver.page_source, 'html.parser')
#table = driver.find_element_by_css_selector("#content > div:nth-child(1) > div > div > table")
table = soup.find('table')
#updated = driver.find_elements_by_class_name("publish-date")
updated = soup.find("p", class_ = "publish-date").getText().strip()
# find all rows
rows = table.findAll('tr')
# init row text array
row_text_array = []
# loop through rows and add row text to array
for row in rows[1:]:
row_text = []
# loop through the elements
for row_element in row.findAll(['th', 'td']):
# append the array with the elements inner text
row_text.append(row_element.text.replace('\n', '').strip())
# append the text array to the row text array
row_text_array.append(row_text)
with open("/home/pi/nndss/data/count_jur.csv", "a") as f:
wr = csv.writer(f)
# loop through each row array
for row_text_single in row_text_array:
wr.writerow(row_text_single + [key, updated])
driver.get("http://www9.health.gov.au/cda/source/rpt_4_sel.cfm")
time.sleep(2)
with open("/home/pi/nndss/data/rate_jur.csv", "w") as f:
wr = csv.writer(f)
wr.writerow(col_headings)
for key in causeList:
report = driver.find_element_by_id("REPORT_OPTION_2")
report.click()
elem = driver.find_element_by_name("CAUSE")
elem.send_keys(causeList[key])
button = driver.find_element_by_name("CTIME1")
button.click()
time.sleep(3)
soup = BeautifulSoup(driver.page_source, 'html.parser')
#table = driver.find_element_by_css_selector("#content > div:nth-child(1) > div > div > table")
table = soup.find('table')
#updated = driver.find_elements_by_class_name("publish-date")
updated = soup.find("p", class_ = "publish-date").getText().strip()
# find all rows
rows = table.findAll('tr')
# init row text array
row_text_array = []
# loop through rows and add row text to array
for row in rows[1:]:
row_text = []
# loop through the elements
for row_element in row.findAll(['th', 'td']):
# append the array with the elements inner text
row_text.append(row_element.text.replace('\n', '').strip())
# append the text array to the row text array
row_text_array.append(row_text)
with open("/home/pi/nndss/data/rate_jur.csv", "a") as f:
wr = csv.writer(f)
# loop through each row array
for row_text_single in row_text_array:
wr.writerow(row_text_single + [key, updated])
driver.get("http://www9.health.gov.au/cda/source/rpt_4_sel.cfm")
time.sleep(2)
############################
############################
col_headings = ['YEAR', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Total', 'condition', 'updated']
with open("/home/pi/nndss/data/count_aus.csv", "w") as f:
wr = csv.writer(f)
wr.writerow(col_headings)
driver.get("http://www9.health.gov.au/cda/source/rpt_3_sel.cfm")
soup = BeautifulSoup(driver.page_source, 'html.parser')
for key in causeList:
report = driver.find_element_by_id("REPORT_OPTION_1")
report.click()
elem = driver.find_element_by_name("CAUSE")
elem.send_keys(causeList[key])
button = driver.find_element_by_name("submit")
button.click()
time.sleep(3)
soup = BeautifulSoup(driver.page_source, 'html.parser')
#table = driver.find_element_by_css_selector("#content > div:nth-child(1) > div > div > table")
table = soup.find('table')
#updated = driver.find_elements_by_class_name("publish-date")
updated = soup.find("p", class_ = "publish-date").getText().strip()
# find all rows
rows = table.findAll('tr')
# init row text array
row_text_array = []
# loop through rows and add row text to array
for row in rows[1:]:
row_text = []
# loop through the elements
for row_element in row.findAll(['th', 'td']):
# append the array with the elements inner text
row_text.append(row_element.text.replace('\n', '').strip())
# append the text array to the row text array
row_text_array.append(row_text)
with open("/home/pi/nndss/data/count_aus.csv", "a") as f:
wr = csv.writer(f)
# loop through each row array
for row_text_single in row_text_array:
wr.writerow(row_text_single + [key, updated])
driver.get("http://www9.health.gov.au/cda/source/rpt_3_sel.cfm")
time.sleep(2)
col_headings = ['YEAR', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'condition', 'updated']
with open("/home/pi/nndss/data/rate_aus.csv", "w") as f:
wr = csv.writer(f)
wr.writerow(col_headings)
for key in causeList:
report = driver.find_element_by_id("REPORT_OPTION_2")
report.click()
elem = driver.find_element_by_name("CAUSE")
elem.send_keys(causeList[key])
button = driver.find_element_by_name("submit")
button.click()
time.sleep(3)
soup = BeautifulSoup(driver.page_source, 'html.parser')
#table = driver.find_element_by_css_selector("#content > div:nth-child(1) > div > div > table")
table = soup.find('table')
#updated = driver.find_elements_by_class_name("publish-date")
updated = soup.find("p", class_ = "publish-date").getText().strip()
# find all rows
rows = table.findAll('tr')
# init row text array
row_text_array = []
# loop through rows and add row text to array
for row in rows[1:]:
row_text = []
# loop through the elements
for row_element in row.findAll(['th', 'td']):
# append the array with the elements inner text
row_text.append(row_element.text.replace('\n', '').strip())
# append the text array to the row text array
row_text_array.append(row_text)
with open("/home/pi/nndss/data/rate_aus.csv", "a") as f:
wr = csv.writer(f)
# loop through each row array
for row_text_single in row_text_array:
wr.writerow(row_text_single + [key, updated])
driver.get("http://www9.health.gov.au/cda/source/rpt_3_sel.cfm")
time.sleep(2)
os.system("cd /home/pi/nndss && git add . && git commit -m 'automated commit' && git push")
print("Completed job without error!")