-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtractData.py
351 lines (303 loc) · 14.8 KB
/
ExtractData.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# Grabs html code from given url
# This solution does work but it is slow
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from bs4 import BeautifulSoup
from Award import Award
counter = 0
def passResult(faculty, type, keyword, amount, indig, inter, disabil, appli, renew):
driver = webdriver.PhantomJS()
result = []
typeList = ["Athletic Award", "Bursary", "Combined Medal and Prize", "Fellowship", "Medal", "Prize", "Scholarship",
"Undergraduate Research Award"]
typeSyn = ["ATHL+AWARD", "BURS", "COMB", "FELLOW", "MEDAL", "PRIZE", "SCHOL", "RESEARCH"]
# Get the original page
for currFaculty in faculty:
for currType in type:
# Max Rady College of Medicine is an exception, it has sub-branches
if currFaculty == "Max Rady College of Medicine":
driver.get("https://wwwapps.cc.umanitoba.ca:8443/searchableAwards/searchForm/awardSearch")
Select(driver.find_element_by_xpath("//*[@id='faculty']")).select_by_visible_text(currFaculty)
Select(driver.find_element_by_xpath("//*[@id='awardsPerPage']")).select_by_visible_text("30")
if indig and indig != "-1":
Select(driver.find_element_by_xpath("//*[@id='aboriginal']")).select_by_visible_text(indig)
if inter and inter != "-1":
Select(driver.find_element_by_xpath("//*[@id='international']")).select_by_visible_text(inter)
if disabil and disabil != "-1":
Select(driver.find_element_by_xpath("//*[@id='disabled']")).select_by_visible_text(disabil)
if appli and appli == "on":
driver.find_element_by_xpath("//*[@id='requireApp']").click()
if currType == "View all types":
for y in typeList:
Select(driver.find_element_by_xpath("//*[@id='type']")).select_by_visible_text(y)
driver.find_element_by_xpath("//*[@id='Search']").click()
driver.find_element_by_link_text("Family Social Sciences").click()
result += extractAwards(driver, keyword, amount, renew) # result on the first page
try:
while driver.find_element_by_link_text("Next"):
driver.find_element_by_link_text("Next").click()
result += extractAwards(driver, keyword, amount, renew)
except NoSuchElementException:
pass
driver.find_element_by_xpath("//*[@id='Search']").click()
driver.find_element_by_link_text("Undergraduate Medical Education").click()
result += extractAwards(driver, keyword, amount, renew) # result on the first page
try:
while driver.find_element_by_link_text("Next"):
driver.find_element_by_link_text("Next").click()
result += extractAwards(driver, keyword, amount, renew)
except NoSuchElementException:
pass
else:
if currType == "Entrance Bursary":
Select(driver.find_element_by_xpath("//*[@id='level']")).select_by_visible_text("Entrance")
Select(driver.find_element_by_xpath("//*[@id='type']")).select_by_visible_text("Bursary")
driver.find_element_by_xpath("//*[@id='Search']").click()
driver.find_element_by_link_text("Family Social Sciences").click()
result += extractAwards(driver, keyword, amount, renew) # result on the first page
try:
while driver.find_element_by_link_text("Next"):
driver.find_element_by_link_text("Next").click()
result += extractAwards(driver, keyword, amount, renew)
except NoSuchElementException:
pass
driver.find_element_by_xpath("//*[@id='Search']").click()
driver.find_element_by_link_text("Undergraduate Medical Education").click()
result += extractAwards(driver, keyword, amount, renew) # result on the first page
try:
while driver.find_element_by_link_text("Next"):
driver.find_element_by_link_text("Next").click()
result += extractAwards(driver, keyword, amount, renew)
except NoSuchElementException:
pass
elif currType == "Entrance Scholarship":
Select(driver.find_element_by_xpath("//*[@id='level']")).select_by_visible_text("Entrance")
Select(driver.find_element_by_xpath("//*[@id='type']")).select_by_visible_text("Scholarship")
driver.find_element_by_xpath("//*[@id='Search']").click()
driver.find_element_by_link_text("Family Social Sciences").click()
result += extractAwards(driver, keyword, amount, renew) # result on the first page
try:
while driver.find_element_by_link_text("Next"):
driver.find_element_by_link_text("Next").click()
result += extractAwards(driver, keyword, amount, renew)
except NoSuchElementException:
pass
driver.find_element_by_xpath("//*[@id='Search']").click()
driver.find_element_by_link_text("Undergraduate Medical Education").click()
result += extractAwards(driver, keyword, amount, renew) # result on the first page
try:
while driver.find_element_by_link_text("Next"):
driver.find_element_by_link_text("Next").click()
result += extractAwards(driver, keyword, amount, renew)
except NoSuchElementException:
pass
else:
Select(driver.find_element_by_xpath("//*[@id='type']")).select_by_visible_text(currType)
driver.find_element_by_xpath("//*[@id='Search']").click()
driver.find_element_by_link_text("Family Social Sciences").click()
result += extractAwards(driver, keyword, amount, renew) # result on the first page
try:
while driver.find_element_by_link_text("Next"):
driver.find_element_by_link_text("Next").click()
result += extractAwards(driver, keyword, amount, renew)
except NoSuchElementException:
pass
driver.find_element_by_xpath("//*[@id='Search']").click()
driver.find_element_by_link_text("Undergraduate Medical Education").click()
result += extractAwards(driver, keyword, amount, renew) # result on the first page
try:
while driver.find_element_by_link_text("Next"):
driver.find_element_by_link_text("Next").click()
result += extractAwards(driver, keyword, amount, renew)
except NoSuchElementException:
pass
else:
driver.get("https://wwwapps.cc.umanitoba.ca:8443/searchableAwards/searchForm/awardSearch")
Select(driver.find_element_by_xpath("//*[@id='faculty']")).select_by_visible_text(currFaculty)
Select(driver.find_element_by_xpath("//*[@id='awardsPerPage']")).select_by_visible_text("30")
if indig and indig != "-1":
Select(driver.find_element_by_xpath("//*[@id='aboriginal']")).select_by_visible_text(indig)
if inter and inter != "-1":
Select(driver.find_element_by_xpath("//*[@id='international']")).select_by_visible_text(inter)
if disabil and disabil != "-1":
Select(driver.find_element_by_xpath("//*[@id='disabled']")).select_by_visible_text(disabil)
if appli and appli == "on":
driver.find_element_by_xpath("//*[@id='requireApp']").click()
if currType == "View all types":
for y in typeList:
Select(driver.find_element_by_xpath("//*[@id='type']")).select_by_visible_text(y)
driver.find_element_by_xpath("//*[@id='Search']").click()
result += extractAwards(driver, keyword, amount, renew) # result on the first page
try:
while driver.find_element_by_link_text("Next"):
driver.find_element_by_link_text("Next").click()
result += extractAwards(driver, keyword, amount, renew)
except NoSuchElementException:
pass
else:
if currType == "Entrance Bursary":
Select(driver.find_element_by_xpath("//*[@id='level']")).select_by_visible_text("Entrance")
Select(driver.find_element_by_xpath("//*[@id='type']")).select_by_visible_text("Bursary")
driver.find_element_by_xpath("//*[@id='Search']").click()
result += extractAwards(driver, keyword, amount, renew) # result on the first page
try:
while driver.find_element_by_link_text("Next"):
driver.find_element_by_link_text("Next").click()
result += extractAwards(driver, keyword, amount, renew)
except NoSuchElementException:
pass
elif currType == "Entrance Scholarship":
Select(driver.find_element_by_xpath("//*[@id='level']")).select_by_visible_text("Entrance")
Select(driver.find_element_by_xpath("//*[@id='type']")).select_by_visible_text("Scholarship")
driver.find_element_by_xpath("//*[@id='Search']").click()
result += extractAwards(driver, keyword, amount, renew) # result on the first page
try:
while driver.find_element_by_link_text("Next"):
driver.find_element_by_link_text("Next").click()
result += extractAwards(driver, keyword, amount, renew)
except NoSuchElementException:
pass
else:
Select(driver.find_element_by_xpath("//*[@id='type']")).select_by_visible_text(currType)
driver.find_element_by_xpath("//*[@id='Search']").click()
result += extractAwards(driver, keyword, amount, renew) # result on the first page
try:
while driver.find_element_by_link_text("Next"):
driver.find_element_by_link_text("Next").click()
result += extractAwards(driver, keyword, amount, renew)
except NoSuchElementException:
pass
return result
def extractAwards(currPage, keyword, amount, renew):
soup = BeautifulSoup(currPage.page_source, "html.parser")
driver2 = webdriver.PhantomJS()
awards = []
results = [] # Contains keyword related results
keywords = [] # Collection of keyword
global counter
type = soup.find(id="award").text.split()
for entry in soup.find_all("div", {"id": "awardInfo"}):
url = "https://wwwapps.cc.umanitoba.ca:8443" + entry.find('a').get('href')
name = entry.find('a').text
spanList = entry.find_all(id="rightTag")
if keyword == "" and amount == "" and renew != "on":
counter += 1
award = Award(url, spanList[0].text, name, type[3], spanList[1].text, spanList[2].text, None, 0, None,
entry.find(id="awardDesc").text.strip()[:-1][1:], counter)
# Both amount and renewable are presented
if amount != "" and renew == "on":
driver2.get(url)
soup2 = BeautifulSoup(driver2.page_source, "html.parser")
div = soup2.find_all('div', class_="rowDisp")
price = div[3].text.strip()[14:][:12].strip()
try: # Make sure get the numerical value not string
renewable = div[2].text.strip()[38:].strip()
if int(price) >= int(amount) and renewable == "Yes":
if keyword == "":
counter += 1
award.sequence = counter
awards.append(award)
except:
pass
# Only amount is presented
elif amount != "" and renew != "on":
driver2.get(url)
soup2 = BeautifulSoup(driver2.page_source, "html.parser")
div = soup2.find_all('div', class_="rowDisp")
price = div[3].text.strip()[14:][:12].strip()
try: # Make sure get the numerical value not string
if int(price) >= int(amount):
if keyword == "":
counter += 1
award.sequence = counter
awards.append(award)
except:
pass
# Only renewable is presented
elif amount == "" and renew == "on":
driver2.get(url)
soup2 = BeautifulSoup(driver2.page_source, "html.parser")
div = soup2.find_all('div', class_="rowDisp")
try: # Make sure get the numerical value not string
renewable = div[2].text.strip()
if renewable == "Yes":
if keyword == "":
counter += 1
award.sequence = counter
awards.append(award)
except:
pass
# Neither amount nor renewable are presented
elif amount == "" and renew != "on":
awards.append(award)
if keyword:
# try to split string based on logical conjunction
# if find logical conjunction AND
if keyword.find("AND") != -1:
keywords = keyword.split("AND")
for award in awards:
isFound = True
awardInfo = str(award.displayAward())
for currKeyword in keywords:
if awardInfo.find(currKeyword.strip()) == -1:
isFound = False
break
if (isFound == True):
counter += 1
award.sequence = counter
results.append(award)
# if find logical conjunction OR
elif keyword.find("OR") != -1:
keywords = keyword.split("OR")
for award in awards:
awardInfo = str(award.displayAward())
for currKeyword in keywords:
if awardInfo.find(currKeyword.strip()) != -1:
counter += 1
award.sequence = counter
results.append(award)
break
# if find logical conjunction NOT
elif keyword.find("NOT") != -1:
keywords = keyword.split("NOT")
for award in awards:
isFound = True
awardInfo = str(award.displayAward())
firstKeyword = keywords[0]
# There is a keyword before logical conjunction NOT
if firstKeyword != "":
if awardInfo.find(firstKeyword.strip()) != -1:
for currKeyword in keywords[1:]: # make sure the keyword after NOT is not included
if awardInfo.find(currKeyword.strip()) != -1:
isFound = False
break
if (isFound == True):
counter += 1
award.sequence = counter
results.append(award)
else:
for currKeyword in keywords[1:]: # make sure the keyword after NOT is not included
if awardInfo.find(currKeyword.strip()) != -1:
isFound = False
break
if (isFound == True):
counter += 1
award.sequence = counter
results.append(award)
# no logic conjunction is found, only single keyword
else:
for award in awards:
awardInfo = str(award.displayAward())
if awardInfo.find(keyword) != -1:
counter += 1
award.sequence = counter
results.append(award)
return results
else:
return awards
def getTotal():
global counter
temp = counter # Pass counter value to temp
counter = 0 # Clear counter after each query
return temp