-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
232 lines (174 loc) · 7.21 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
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
import json
import os.path
import re
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from manage_csv import save_csv
from maps import Maps
LIST_URL = "https://www.toronto.ca/data/parks/prd/facilities/recreationcentres/index.html"
ITEM_URL_TEMPLATE = "https://www.toronto.ca{}"
JSON_FILE = "Recreation_Centres_Info.json"
CACHE_DIR = "data/cache"
PROGRAM = os.getenv('default_program')
class NotFoundProgramException(Exception):
pass
class InvalidValueError(ValueError):
pass
class BaseScraper:
def __init__(self):
# initiating the webdriver. Parameter includes the path of the webdriver.
options = Options()
options.add_argument("start-maximized")
options.add_argument("--headless")
options.add_argument("--no-sandbox")
dir_path = os.path.dirname(os.path.realpath(__file__))
service = Service(f'{dir_path}/tool/chromedriver')
self.driver = webdriver.Chrome(service=service, options=options)
class ListScraper(BaseScraper):
def __call__(self, url):
self.driver.get(url)
# this is just to ensure that the page is completely loaded
time.sleep(5)
html = self.driver.page_source
soup = BeautifulSoup(html, "html.parser")
items = soup.tbody.find_all("tr")
for i, item in enumerate(items):
title, map_link = item.find_all("a")
address = item.find("td", {"data-info": "Address"})
yield {
"id": i + 1,
"title": title.get_text(strip=True),
"address": address.get_text(strip=True),
"url": title['href'],
"map_link": map_link['href'],
}
self.driver.quit() # closing the webdriver
class ItemScraper(BaseScraper):
def __call__(self, url, program=PROGRAM):
print(f'[Parsing] {url}')
self.driver.get(url)
# this is just to ensure that the page is completely loaded
time.sleep(5)
html = self.driver.page_source
soup = BeautifulSoup(html, "html.parser")
try:
rec_centre = soup.find("div", {"class": "accbox"}).find("h1")
drop_in = soup.find("div", {"id": "pfrComplexTabs-dropin"})
sports_tab = drop_in.find("tr", {"id": "dropin_Sports_0"})
sports = sports_tab.tbody.find_all("tr")
days = [d.get_text(strip=True) for d in sports_tab.thead.find_all("th")]
days.pop(0)
programs_count = 0
for sport in sports:
program_span = sport.find("span", {"class": "coursetitlecol"})
if program_span.get_text() == program:
age = program_span.find_next_sibling("span").get_text(strip=True)
age = age[1:-1]
if not self._is_adult(age):
continue
for i, timeslot_td in enumerate(sport.find_all("td")):
timeslot = timeslot_td.get_text(strip=True)
if timeslot:
programs_count += 1
yield {
"location": rec_centre.get_text(strip=True),
"day": days[i],
"timeslot": timeslot,
"age": age,
"url": url,
}
if programs_count == 0:
raise NotFoundProgramException(f"No Drop-in {program} programs found.")
except NotFoundProgramException as e:
print(f"{e} No relevant data exist. Exit gracefully.")
except Exception as e:
print(f"{e} Something wrong happens during parsing data. Exit gracefully.")
self.driver.quit() # closing the webdriver
def _is_adult(self, age_string):
age_string = age_string.replace("yrs", "")
numbers = []
for word in age_string.split():
if word.isdigit():
numbers.append(int(word))
if len(numbers) >= 2 and numbers[-1] >= 18:
return True
elif len(numbers) == 1 and 'over' in age_string:
return True
return False
def scrape_list():
it = ListScraper()(LIST_URL)
rec_centres = {'rec_centres': []}
while True:
try:
data = next(it)
rec_centres['rec_centres'].append(data)
except StopIteration:
break
write_json(rec_centres)
print("List of Recreation Centres has been initialized.")
def scrape_item(url, program=PROGRAM):
url = ITEM_URL_TEMPLATE.format(url)
it = ItemScraper()(url, program)
programs = []
while True:
try:
data = next(it)
programs.append(data)
except StopIteration:
break
print(f"[Done] {url}")
return programs
# function to add to JSON
def write_json(python_obj, filename=JSON_FILE, mode='w', indent=4):
with open(filename, mode) as f:
json.dump(python_obj, f, indent=indent)
def is_valid_postcode(postcode):
result = re.findall(r'[a-z]{1}[0-9]{1}[a-z]{1}\s*[0-9]{1}[a-z]{1}[0-9]{1}', postcode)
if len(result) == 1 and len(postcode) == 6:
return True
return False
def scrape_manager(postcode, program=PROGRAM, limit=5):
if not is_valid_postcode(postcode):
raise ValueError('Error: invalid postcode')
postcode_cache_filename = f'{CACHE_DIR}/{postcode}.json'
try:
with open(postcode_cache_filename) as f:
sorted_rec_centres = json.load(f)
except FileNotFoundError:
# only run when a postcode cache info doesn't exist
if not os.path.exists(JSON_FILE):
scrape_list()
with open(JSON_FILE) as f:
rec_centres = json.load(f)
m = Maps()
# point to point information (zipcode to recreation centres)
p2p_info_list = []
for rec_centre in rec_centres['rec_centres']:
dest = f"{rec_centre['address']} Toronto"
distance = m.calc_distance(postcode, dest)
p2p_info_list.append({
'id': rec_centre['id'],
'distance': distance,
'url': rec_centre['url'],
})
sorted_rec_centres = sorted(p2p_info_list, key=lambda d: d['distance'])
write_json(sorted_rec_centres, postcode_cache_filename)
programs_json = {program: []}
for i, centre in enumerate(sorted_rec_centres):
if limit:
results = scrape_item(centre['url'], program)
print(f'>>> {i+1} recreation centre(s) parsed.')
limit -= 1
if len(results):
programs_json[program].extend(results)
write_json(programs_json, f'data/output/{postcode}-{program}.json')
save_csv(f'{postcode}-{program}', programs_json[program])
return programs_json[program]
# simple tests
# scrape_list()
# scrape_item("/data/parks/prd/facilities/complex/13/index.html", "Volleyball")
# scrape_manager("m5t1g4")
# scrape_manager("m5v0r6", program="Badminton", limit=164)