-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMigrationDataScraper.py
More file actions
166 lines (132 loc) · 6.67 KB
/
MigrationDataScraper.py
File metadata and controls
166 lines (132 loc) · 6.67 KB
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
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
############################### DUMMY INPUTS ###################################
dataset='S7_student_first_time'
variable_list = ['Location','Institution Type','Nationality','Application Criteria']
default_selection = False
filters = [
{
'FilterVariable' : 'Date',
'FilterType' : "DOES NOT CONTAIN",
'FilterCriteria' : ['2013-07-31','2013-09-30']
},
{
'FilterVariable' : 'Student Type',
'FilterType' : "DOES NOT CONTAIN",
'FilterCriteria' : ['Other']
},
{
'FilterVariable' : 'Location',
'FilterType' : "CONTAINS",
'FilterCriteria' : ['Auckland','Bay of Plenty']
},
{
'FilterVariable' : 'Nationality',
'FilterType' : "CONTAINS",
'FilterCriteria' : ['Pakistan','Bangladesh']
}
]
################################ FUNCTION ###################################
def MigrationDataScraper(dataset,variable_list,filters,default_selection):
options=webdriver.ChromeOptions()
options.add_experimental_option('detach',True)
driver = webdriver.Chrome(options=options,service=ChromeService(ChromeDriverManager().install()))
driver.get("https://mbienz.shinyapps.io/migration_data_explorer/")
element = driver.find_element(By.LINK_TEXT, 'Data Explorer')
element.click()
#category selection
categories=driver.find_elements(By.NAME,'data_explorer-dname')
for category in categories:
if (category.get_attribute('value') == dataset):
category.click()
time.sleep(2)
#variables selection
div_element = driver.find_element(By.CLASS_NAME, 'list-group.inlist.inlist-multi')
variables = div_element.find_elements(By.TAG_NAME, 'button')
if (default_selection and len(variable_list)<=3): # Max 4 variables allowed
for variable in variables:
if(variable.get_attribute('value') in variable_list):
variable.click()
elif (len(variable_list)<=4 and not default_selection):
for variable in variables:
if(variable.get_attribute('class') == 'list-group-item list-group-item-selected'):
first_span = variable.find_element(By.XPATH, "./span[1]")
driver.execute_script("arguments[0].setAttribute('class', 'glyphicon glyphicon-unchecked')", first_span)
variable.click()
break
for variable in variables:
if(variable.get_attribute('value') in variable_list):
variable.click()
#Filters selection
if (filters and len(filters)):
for i in range (len(filters)):
filter_var = filters[i]['FilterVariable']
filter_type = filters[i]['FilterType']
filter_criteria = filters[i]['FilterCriteria']
filter_div = driver.find_element(By.ID, 'data_explorer-cond-add')
add_filter_btn = filter_div.find_element(By.TAG_NAME, 'button')
add_filter_btn.click()
time.sleep(0.5)
if (filter_var == 'Date'):
if (filter_type == 'DOES NOT CONTAIN'):
filter_type_div = driver.find_element(By.ID,'data_explorer-cond-type')
conditions = filter_type_div.find_elements(By.XPATH, ".//div[@class='radio']//input")
time.sleep(0.5)
for con in conditions:
if con.get_attribute('value') == filter_type:
con.click()
for j in range(len(filter_criteria)):
filter_criteria_div = driver.find_element(By.CSS_SELECTOR, 'div.list-group.inlist.inlist-multi[data-inlist="never-toggle"]')
filter_btn_tags = filter_criteria_div.find_elements(By.TAG_NAME, 'button')
time.sleep(0.5)
for flt in filter_btn_tags:
if(flt.get_attribute('value') == filter_criteria[j]):
flt.click()
break
submit_btn = driver.find_element(By.CSS_SELECTOR, 'div.float-con button.btn.btn-default.btn-ok')
submit_btn.click()
if (filter_var != 'Date'):
filter_variable_div = driver.find_element(By.ID,'data_explorer-cond-var')
options = filter_variable_div.find_elements(By.XPATH, ".//div[@class='radio']//input")
time.sleep(0.5)
for opt in options:
if opt.get_attribute('value') == filter_var:
opt.click()
break
filter_type_div = driver.find_element(By.ID,'data_explorer-cond-type')
conditions = filter_type_div.find_elements(By.XPATH, ".//div[@class='radio']//input")
time.sleep(0.5)
for con in conditions:
if con.get_attribute('value') == filter_type:
con.click()
for j in range(len(filter_criteria)):
filter_criteria_div = driver.find_element(By.CSS_SELECTOR, 'div.list-group.inlist.inlist-multi[data-inlist="never-toggle"]')
filter_btn_tags = filter_criteria_div.find_elements(By.TAG_NAME, 'button')
time.sleep(0.5)
for flt in filter_btn_tags:
if(flt.get_attribute('value') == filter_criteria[j]):
flt.click()
break
submit_btn = driver.find_element(By.CSS_SELECTOR, 'div.float-con button.btn.btn-default.btn-ok')
submit_btn.click()
time.sleep(2)
download_btn=driver.find_element(By.ID,'data_explorer-csv-down')
download_btn.click()
################### FUNCTION CALLS ###################################
# USE CASE I: Select dataset S7 Student First Time-->Time Period Monthly -->
# Variable Application criteria, Nationality, Location & Institution type
dataset_1='S7_student_first_time'
variable_list_1 = ['Nationality','Location','Institution Type']
default_selection_1 = True
filters_1 = []
#MigrationDataScraper(dataset_1,variable_list_1,filters_1,default_selection_1)
# USE CASE II: Select dataset S1 Student Decisions--> Time Period Monthly-->
# Variable Decision type, Nationality, Institution type & location
dataset_2='S1_student_decisions'
variable_list_2 = ['Nationality','Institution Type','Location']
default_selection_2 = True
filters_2 = []
#MigrationDataScraper(dataset_2,variable_list_2,filters_2,default_selection_2)