-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChandler-Apartment-Scraper.py
399 lines (249 loc) · 11.9 KB
/
Chandler-Apartment-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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from selenium import webdriver
import pandas as pd
from time import sleep
import datetime
from bs4 import BeautifulSoup
import pynput.mouse as ms
import pynput.keyboard as kb
mouse = ms.Controller()
keyboard = kb.Controller()
# In[2]:
# Load Selenium Webdriver
#driver = webdriver.Chrome(r'C:\Users\jcgou\Desktop\Chandler-Apartment-Scraper\Chrome Driver\chromedriver.exe')
driver = webdriver.Chrome(r'C:\Users\jamesgou\Desktop\Chandler-Apartment-Scraper\Chrome Driver\chromedriver.exe')
print('Loading Selenium Webdriver')
# In[3]:
# Create CSV file for the pricing information
#filename = 'Chandler Apartment Pricing.csv'
f = open(r'C:\Users\jamesgou\Desktop\Chandler-Apartment-Scraper\Chandler Apartment Pricing.csv', 'a')
#headers = 'Date, Apartment Complex Name, Unit, Price\n'
#f.write(headers)
# In[4]:
# Record Today's date
date_object = datetime.date.today()
print("Today's date is: " + str(date_object))
# In[5]:
# Navigate to 1st Apartment website - San Brisas
san_brisas_url = 'https://www.sanbrisasapts.com/floor-plans.aspx'
driver.get(san_brisas_url)
driver.maximize_window()
print('Maximizing Chrome Driver')
# In[6]:
try:
# Save HTML and extract portfolio recommendation information
san_brisas_html = driver.page_source
san_brisas_soup = BeautifulSoup(san_brisas_html, 'lxml')
# Extract apartment A2 price
san_brisas_a2_price = san_brisas_soup.find(id = 'fp_6830196_range').get_text()
print("The San Brisas A2 price is currently " + san_brisas_a2_price)
san_brisas_a2_price = san_brisas_a2_price.replace(',', '')
# Write San Brisas prices to CSV file
f.write(str(date_object) + ',' + 'San Brisas' + ',' + 'A2' + ',' + str(san_brisas_a2_price) + '\n')
print('San Brisas prices written')
except:
# Write San Brisas prices to CSV file
f.write(str(date_object) + ',' + 'San Brisas' + ',' + 'A2' + ',' + 'Error' + '\n')
print('San Brisas prices written')
# In[7]:
# Navigate to 2nd apartment website
country_brook_url = 'https://countrybrook.mgproperties.com/brochure.aspx'
driver.get(country_brook_url)
print('Navigating to next apartment, Country Brook')
# In[8]:
# Save HTML and extract portfolio recommendation information
country_brook_html = driver.page_source
country_brook_soup = BeautifulSoup(country_brook_html, 'lxml')
# In[9]:
unit_list = []
price_list = []
table = country_brook_soup.table
table_rows = table.find_all('tr')
for tr in table_rows[1:8]:
td = tr.find_all('td')
row = [i.text for i in td]
unit_list.append(row[1])
price_list.append(row[5])
#print(row)
final_unit_list = []
for i in unit_list:
final_unit_list.append(i.strip())
country_brook_dict = dict(zip(final_unit_list, price_list))
print("The Country Brook prices are currently: ")
print(country_brook_dict)
# In[10]:
# Write Country Brook prices to CSV file
cb_a1_price = country_brook_dict['A1'].replace(',','')
cb_a2_price = country_brook_dict['A2'].replace(',','')
cb_a3_price = country_brook_dict['A3'].replace(',','')
cb_a4_price = country_brook_dict['A4'].replace(',','')
cb_b1_price = country_brook_dict['B1'].replace(',','')
cb_b2_price = country_brook_dict['B2'].replace(',','')
f.write(str(date_object) + ',' + 'Country Brook' + ',' + 'A1' + ',' + str(cb_a1_price) + '\n')
f.write(str(date_object) + ',' + 'Country Brook' + ',' + 'A2' + ',' + str(cb_a2_price) + '\n')
f.write(str(date_object) + ',' + 'Country Brook' + ',' + 'A3' + ',' + str(cb_a3_price) + '\n')
f.write(str(date_object) + ',' + 'Country Brook' + ',' + 'A4' + ',' + str(cb_a4_price) + '\n')
f.write(str(date_object) + ',' + 'Country Brook' + ',' + 'B1' + ',' + str(cb_b1_price) + '\n')
f.write(str(date_object) + ',' + 'Country Brook' + ',' + 'B2' + ',' + str(cb_b2_price) + '\n')
print('Country Brook prices written')
# In[11]:
# Navigate to 3rd apartment website
san_tierra_url = 'https://www.liveatsantierra.com/floorplans'
driver.get(san_tierra_url)
print('Navigating to next apartment, San Tierra')
# In[12]:
# Save HTML and extract portfolio recommendation information
san_tierra_html = driver.page_source
san_tierra_soup = BeautifulSoup(san_tierra_html, 'lxml')
# In[13]:
# Extract apartment price 1 bed 1 bath 801 sqft
san_tierra_1x1_price = san_tierra_soup.find(id = '2391451').get_text()
san_tierra_1x1_price = san_tierra_1x1_price.split()
san_tierra_1x1_part_a = san_tierra_1x1_price[0:6]
san_tierra_1x1_part_a = "".join(san_tierra_1x1_price)[0:20]
print(san_tierra_1x1_part_a)
san_tierra_1x1_part_b = san_tierra_1x1_price[15]
print(san_tierra_1x1_part_b)
san_tierra_1x1_part_b = san_tierra_1x1_part_b.replace(',', '')
san_tierra_1x1_part_b = san_tierra_1x1_part_b.replace('.00', '')
# In[14]:
# Extract apartment price 2 bed 2 bath 1005 sqft
san_tierra_2x2a_price = san_tierra_soup.find(id = '2391452').get_text()
san_tierra_2x2a_price = san_tierra_2x2a_price.split()
san_tierra_2x2a_part_a = san_tierra_2x2a_price[0:6]
san_tierra_2x2a_part_a = "".join(san_tierra_2x2a_price)[0:20]
print(san_tierra_2x2a_part_a)
san_tierra_2x2a_part_b = san_tierra_2x2a_price[15]
print(san_tierra_2x2a_part_b)
san_tierra_2x2a_part_b = san_tierra_2x2a_part_b.replace(',', '')
san_tierra_2x2a_part_b = san_tierra_2x2a_part_b.replace('.00', '')
# In[15]:
# Extract apartment price 2 bed 2 bath 1050 sqft
san_tierra_2x2b_price = san_tierra_soup.find(id = '2391453').get_text()
san_tierra_2x2b_price = san_tierra_2x2b_price.split()
san_tierra_2x2b_part_a = san_tierra_2x2b_price[0:6]
san_tierra_2x2b_part_a = "".join(san_tierra_2x2b_price)[0:20]
print(san_tierra_2x2b_part_a)
san_tierra_2x2b_part_b = san_tierra_2x2b_price[15]
print(san_tierra_2x2b_part_b)
san_tierra_2x2b_part_b = san_tierra_2x2b_part_b.replace(',', '')
san_tierra_2x2b_part_b = san_tierra_2x2b_part_b.replace('.00', '')
# In[16]:
# Extract apartment price 2 bed 2 bath 1082 sqft
san_tierra_2x2c_price = san_tierra_soup.find(id = '2391454').get_text()
san_tierra_2x2c_price = san_tierra_2x2c_price.split()
san_tierra_2x2c_part_a = san_tierra_2x2c_price[0:6]
san_tierra_2x2c_part_a = "".join(san_tierra_2x2c_price)[0:20]
print(san_tierra_2x2c_part_a)
san_tierra_2x2c_part_b = san_tierra_2x2c_price[15]
print(san_tierra_2x2a_part_b)
san_tierra_2x2c_part_b = san_tierra_2x2c_part_b.replace(',', '')
san_tierra_2x2c_part_b = san_tierra_2x2c_part_b.replace('.00', '')
# In[17]:
# Write San Tierra prices to CSV file
f.write(str(date_object) + ',' + 'San Tierra' + ',' + str(san_tierra_1x1_part_a) + ',' + str(san_tierra_1x1_part_b) + '\n')
f.write(str(date_object) + ',' + 'San Tierra' + ',' + str(san_tierra_2x2a_part_a) + ',' + str(san_tierra_2x2a_part_b) + '\n')
f.write(str(date_object) + ',' + 'San Tierra' + ',' + str(san_tierra_2x2b_part_a) + ',' + str(san_tierra_2x2b_part_b) + '\n')
f.write(str(date_object) + ',' + 'San Tierra' + ',' + str(san_tierra_2x2c_part_a) + ',' + str(san_tierra_2x2c_part_b) + '\n')
print('San Tierra prices written')
# In[18]:
# Navigate to 4th apartment website
reflections_gilasprings_url = 'https://www.reflectionsatgilasprings.com/floorplans'
driver.get(reflections_gilasprings_url)
print('Navigating to next apartment, Reflections at Gila Springs')
# In[19]:
# Save HTML and extract portfolio recommendation information
reflections_gilasprings_html = driver.page_source
reflections_gilasprings_soup = BeautifulSoup(reflections_gilasprings_html, 'lxml')
# In[20]:
# Extract apartment price 1 bed 1 bath 824 sqft
reflections_1x1a_price = reflections_gilasprings_soup.find(id = '2421640').get_text()
reflections_1x1a_price = reflections_1x1a_price.split()
reflections_1x1a_part_a = reflections_1x1a_price[0:6]
reflections_1x1a_part_a = "".join(reflections_1x1a_price)[0:20]
print(reflections_1x1a_part_a)
reflections_1x1a_part_b = reflections_1x1a_price[14]
print(reflections_1x1a_part_b)
reflections_1x1a_part_b = reflections_1x1a_part_b.replace(',', '')
reflections_1x1a_part_b = reflections_1x1a_part_b.replace('.00', '')
# In[21]:
# Extract apartment price 1 bed 1 bath 949 sqft
reflections_1x1b_price = reflections_gilasprings_soup.find(id = '2421641').get_text()
reflections_1x1b_price = reflections_1x1b_price.split()
reflections_1x1b_part_a = reflections_1x1b_price[0:6]
reflections_1x1b_part_a = "".join(reflections_1x1b_price)[0:20]
print(reflections_1x1b_part_a)
reflections_1x1b_part_b = reflections_1x1b_price[14]
print(reflections_1x1b_part_b)
reflections_1x1b_part_b = reflections_1x1b_part_b.replace(',', '')
reflections_1x1b_part_b = reflections_1x1b_part_b.replace('.00', '')
# In[22]:
# Extract apartment price 2 bed 2 bath 1186 sqft
reflections_2x2a_price = reflections_gilasprings_soup.find(id = '2421643').get_text()
reflections_2x2a_price = reflections_2x2a_price.split()
reflections_2x2a_part_a = reflections_2x2a_price[0:6]
reflections_2x2a_part_a = "".join(reflections_2x2a_price)[0:20]
print(reflections_2x2a_part_a)
reflections_2x2a_part_b = reflections_2x2a_price[14]
print(reflections_2x2a_part_b)
reflections_2x2a_part_b = reflections_2x2a_part_b.replace(',', '')
reflections_2x2a_part_b = reflections_2x2a_part_b.replace('.00', '')
# In[23]:
# Extract apartment price 2 bed 2 bath 1156 sqft
reflections_2x2b_price = reflections_gilasprings_soup.find(id = '2421642').get_text()
reflections_2x2b_price = reflections_2x2b_price.split()
reflections_2x2b_part_a = reflections_2x2b_price[0:6]
reflections_2x2b_part_a = "".join(reflections_2x2b_price)[0:20]
print(reflections_2x2b_part_a)
reflections_2x2b_part_b = reflections_2x2b_price[14]
print(reflections_2x2b_part_b)
reflections_2x2b_part_b = reflections_2x2b_part_b.replace(',', '')
reflections_2x2b_part_b = reflections_2x2b_part_b.replace('.00', '')
# In[24]:
# Write Reflections prices to CSV file
f.write(str(date_object) + ',' + 'Reflections at Gila Springs' + ',' + str(reflections_1x1a_part_a) + ',' + str(reflections_1x1a_part_b) + '\n')
f.write(str(date_object) + ',' + 'Reflections at Gila Springs' + ',' + str(reflections_1x1b_part_a) + ',' + str(reflections_1x1b_part_b) + '\n')
f.write(str(date_object) + ',' + 'Reflections at Gila Springs' + ',' + str(reflections_2x2a_part_a) + ',' + str(reflections_2x2a_part_b) + '\n')
f.write(str(date_object) + ',' + 'Reflections at Gila Springs' + ',' + str(reflections_2x2b_part_a) + ',' + str(reflections_2x2b_part_b) + '\n')
print('Reflections at Gila Springs prices written')
# In[25]:
# Navigate to 5th apartment website
the_ventura_url = 'https://www.theventuraapts.com/Floor-plans.aspx'
driver.get(the_ventura_url)
print('Navigating to last apartment, The Ventura')
# In[26]:
# Save HTML and extract portfolio recommendation information
the_ventura_html = driver.page_source
the_ventura_soup = BeautifulSoup(the_ventura_html, 'lxml')
# In[27]:
# Extract apartment A1 price 1 bed 1 bath 798 sqft
ventura_1x1_price = the_ventura_soup.find(id = 'floorplan_7459888').get_text()
ventura_1x1_price = ventura_1x1_price.split()
ventra_1x1_part_a = ventura_1x1_price[1:8]
ventura_1x1_part_a = "".join(ventura_1x1_price)[12:33]
print(ventura_1x1_part_a)
ventura_1x1_part_b = ventura_1x1_price[10:13]
ventura_1x1_part_b = "".join(ventura_1x1_part_b)
ventura_1x1_part_b = ventura_1x1_part_b.replace(',','')
print(ventura_1x1_part_b)
# In[28]:
# Extract apartment B2 price 1 bed 1 bath 1039 sqft
ventura_2x2_price = the_ventura_soup.find(id = 'floorplan_7459889').get_text()
ventura_2x2_price = ventura_2x2_price.split()
ventra_2x2_part_a = ventura_2x2_price[1:10]
ventura_2x2_part_a = "".join(ventura_2x2_price)[12:35]
ventura_2x2_part_a = ventura_2x2_part_a.replace(',','')
print(ventura_2x2_part_a)
ventura_2x2_part_b = ventura_2x2_price[10]
ventura_2x2_part_b = ventura_2x2_part_b.replace(',', '')
print(ventura_2x2_part_b)
# In[29]:
# Write Ventura prices to CSV file
f.write(str(date_object) + ',' + 'The Ventura' + ',' + str(ventura_1x1_part_a) + ',' + str(ventura_1x1_part_b) + '\n')
f.write(str(date_object) + ',' + 'The Ventura' + ',' + str(ventura_2x2_part_a) + ',' + str(ventura_2x2_part_b) + '\n')
print('The Ventura prices written')
# In[30]:
print("Finished!")
exit()