-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scraper.py
193 lines (159 loc) · 6.66 KB
/
Scraper.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
from selenium import webdriver
import re
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import DesiredCapabilities
from bs4 import BeautifulSoup
import time
#**************************************************************************************************
# submitForm
#
# submit the initial form to usaswimming
#
#**************************************************************************************************
def submitForm(firstName, lastName, driver):
try:
fname_field = driver.find_element_by_name('FirstName')
lname_field = driver.find_element_by_name('LastName')
startDate = driver.find_element_by_name('UsasTimeSearchIndividual_Index_Div_1StartDate')
endDate = driver.find_element_by_name('UsasTimeSearchIndividual_Index_Div_1EndDate')
submit = driver.find_element_by_id('UsasTimeSearchIndividual_Index_Div_1-saveButton')
fname_field.send_keys(firstName)
lname_field.send_keys(lastName)
submit.click()
except:
print("There has been a problem submitting form to database. Try again")
#**************************************************************************************************
# scrapeAndSave
#
# Perform the actual time scraping and save to txt file... eventually a database
#
#**************************************************************************************************
def scrapeAndSave(driver):
pageNum = 1
numPages = int(driver.find_element_by_id("UsasTimeSearchIndividual_TimeResults_Grid-1-UsasGridPager-lblTotalPages").text)
nxtPageBtn = driver.find_element_by_id("UsasTimeSearchIndividual_TimeResults_Grid-1-UsasGridPager-pgNext")
browserPgNum = driver.find_element_by_id("UsasTimeSearchIndividual_TimeResults_Grid-1-UsasGridPager-txtCurrentPage")
numPages = 1 #TODO temporary
while(pageNum <= numPages):
bsObj = BeautifulSoup(driver.page_source, "lxml")
#parse table
eventTable = bsObj.find("div", {"class":"k-grid-content-locked"}).table.tbody
dataTable = bsObj.find("tbody", {'role':'rowgroup'})
print(eventTable)
print()
print(dataTable)
print()
print()
pageNum += 1
if(pageNum <= numPages):
nxtPageBtn.click()
#wait for update
for i in range(50):
try:
current = int(browserPgNum.get_attribute('value'))
except:
print("value was none")
if(current == pageNum):
break
if(i == 49):
print("Page load timed out")
return False
else:
time.sleep(0.1)
return True
#**************************************************************************************************
# getTimes
#
# Retrieve times from usaswimming database based on name only
#
#**************************************************************************************************
def getTimes(firstName, lastName):
success = False
#header = DesiredCapabilities.PHANTOMJS.copy()
#header['phantomjs.page.customHeaders.User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) ' \
# 'AppleWebKit/537.36 (KHTML, like Gecko) ' \
# 'Chrome/39.0.2171.95 Safari/537.36'
#driver = webdriver.PhantomJS(executable_path='C:\Phantom\phantomjs.exe', desired_capabilities=header)
driver = webdriver.Chrome(executable_path="C:\Chrome\chromedriver.exe")
driver.get("https://www.usaswimming.org/Home/times/individual-times-search")
#Submit the initial form
submitForm(firstName, lastName, driver)
time.sleep(1) #wait for the response
pageSource = driver.page_source
bsObj = BeautifulSoup(pageSource, "lxml")
clubTable = None
try:
clubTable = driver.find_element_by_id("UsasTimeSearchIndividual_PersonSearchResults_Grid-1")
except:
print("swimmer found?")
table = bsObj.find("tbody", {"role":"rowgroup"})
if(clubTable is not None):
count = 0
for row in table.children:
print(str(count) + " " + row.td.next_sibling.text)
count += 1
print("")
print(count)
success = False
else:
print("User found")
success = scrapeAndSave(driver)
driver.close()
return success
#End getTimes
#**************************************************************************************************
# getTimes_club
#
# Retrieve times from the usaswimming database based on name and team
#
#**************************************************************************************************
def getTimes_club(firstName, lastName, clubID):
success = False
#header = DesiredCapabilities.PHANTOMJS.copy()
#header['phantomjs.page.customHeaders.User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) ' \
# 'AppleWebKit/537.36 (KHTML, like Gecko) ' \
# 'Chrome/39.0.2171.95 Safari/537.36'
#driver = webdriver.PhantomJS(executable_path='C:\Phantom\phantomjs.exe', desired_capabilities=header)
driver = webdriver.Chrome(executable_path="C:\Chrome\chromedriver.exe")
driver.get("https://www.usaswimming.org/Home/times/individual-times-search")
#submit the initial form
submitForm(firstName, lastName, driver)
time.sleep(1)
try:
links = driver.find_elements_by_class_name('pointer')
except:
print("no pointers")
return False
if(len(links) <= clubID):
print("Invalid Club ID")
return False
links[clubID].click()
time.sleep(1)
#get and store time information
success = scrapeAndSave(driver)
input("press key to continue")
driver.close()
return success
#End getTimes
#**************************************************************************************************
# Main - test
#
# Test the functionality above
#
#**************************************************************************************************
firstname = "Alex"
lastname = "Ellison"
success = getTimes(firstname, lastname)
clubID = 1
if(success):
print("data retrieved successfully")
else:
if(getTimes_club(firstname, lastname, clubID)):
print("Woohoo it worked")
else:
print("nope it be broked")
exit