forked from ChrisMuir/Zillow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzillow_functions.py
322 lines (298 loc) · 11 KB
/
zillow_functions.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
# -*- coding: utf-8 -*-
# Zillow scraper functions, these are sourced at the top of zillow_runfile.py
import re as re
import time
import zipcode
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
def zipcodes_list(st_items):
# If st_items is a single zipcode string.
if type(st_items) == str:
zc_objects = zipcode.islike(st_items)
output = [str(i).split(" ", 1)[1].split(">")[0]
for i in zc_objects]
# If st_items is a list of zipcode strings.
elif type(st_items) == list:
zc_objects = [n for i in st_items for n in zipcode.islike(str(i))]
output = [str(i).split(" ", 1)[1].split(">")[0]
for i in zc_objects]
else:
raise ValueError("input 'st_items' must be of type str or list")
return(output)
def init_driver(file_path):
# Starting maximized fixes https://github.com/ChrisMuir/Zillow/issues/1
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path=file_path, chrome_options=options)
driver.wait = WebDriverWait(driver, 10)
return(driver)
def navigate_to_website(driver, site):
driver.get(site)
def click_buy_button(driver):
try:
button = driver.wait.until(EC.element_to_be_clickable(
(By.CLASS_NAME, "nav-header")))
button.click()
time.sleep(10)
except (TimeoutException, NoSuchElementException):
raise ValueError("Clicking the 'Buy' button failed")
def enter_search_term(driver, search_term):
try:
search_bar = driver.wait.until(EC.presence_of_element_located(
(By.ID, "citystatezip")))
button = driver.wait.until(EC.element_to_be_clickable(
(By.CLASS_NAME, "zsg-icon-searchglass")))
search_bar.clear()
time.sleep(3)
search_bar.send_keys(search_term)
time.sleep(3)
button.click()
time.sleep(3)
return(True)
except (TimeoutException, NoSuchElementException):
return(False)
def results_test(driver):
# Check to see if there are any returned results
try:
no_results = driver.find_element_by_css_selector(
'.zoom-out-message').is_displayed()
except (NoSuchElementException, TimeoutException):
# Check to see if the zipcode is invalid or not
try:
no_results = driver.find_element_by_class_name(
'zsg-icon-x-thick').is_displayed()
except (NoSuchElementException, TimeoutException):
no_results = False
return(no_results)
def get_html(driver):
output = []
keep_going = True
while keep_going:
# Pull page HTML
try:
output.append(driver.page_source)
except TimeoutException:
pass
try:
# Check to see if a "next page" link exists
keep_going = driver.find_element_by_class_name(
'zsg-pagination-next').is_displayed()
except NoSuchElementException:
keep_going = False
if keep_going:
# Test to ensure the "updating results" image isnt displayed.
# Will try up to 5 times before giving up, with a 5 second wait
# between each try.
tries = 5
try:
cover = driver.find_element_by_class_name(
'list-loading-message-cover').is_displayed()
except (TimeoutException, NoSuchElementException):
cover = False
while cover and tries > 0:
time.sleep(5)
tries -= 1
try:
cover = driver.find_element_by_class_name(
'list-loading-message-cover').is_displayed()
except (TimeoutException, NoSuchElementException):
cover = False
# If the "updating results" image is confirmed to be gone
# (cover == False), click next page. Otherwise, give up on trying
# to click thru to the next page of house results, and return the
# results that have been scraped up to the current page.
if cover == False:
try:
driver.wait.until(EC.element_to_be_clickable(
(By.CLASS_NAME, 'zsg-pagination-next'))).click()
time.sleep(3)
except TimeoutException:
keep_going = False
else:
keep_going = False
return(output)
def get_listings(list_obj):
# Split the raw HTML into segments, one for each listing.
output = []
for i in list_obj:
htmlSplit = i.split('" id="zpid_')[1:]
output += htmlSplit
print(str(len(output)) + " home listings scraped\n***")
return(output)
def get_street_address(soup_obj):
try:
street = soup_obj.find(
"span", {"itemprop" : "streetAddress"}).get_text().strip()
except (ValueError, AttributeError):
street = "NA"
if len(street) == 0 or street == "null":
street = "NA"
return(street)
def get_city(soup_obj):
try:
city = soup_obj.find(
"span", {"itemprop" : "addressLocality"}).get_text().strip()
except (ValueError, AttributeError):
city = "NA"
if len(city) == 0 or city == "null":
city = "NA"
return(city)
def get_state(soup_obj):
try:
state = soup_obj.find(
"span", {"itemprop" : "addressRegion"}).get_text().strip()
except (ValueError, AttributeError):
state = "NA"
if len(state) == 0 or state == 'null':
state = "NA"
return(state)
def get_zipcode(soup_obj):
try:
zipcode = soup_obj.find(
"span", {"itemprop" : "postalCode"}).get_text().strip()
except (ValueError, AttributeError):
zipcode = "NA"
if len(zipcode) == 0 or zipcode == 'null':
zipcode = "NA"
return(zipcode)
def get_price(soup_obj, list_obj):
# Look for price within the BeautifulSoup object.
try:
price = soup_obj.find(
"span", {"class" : "zsg-photo-card-price"}).get_text().strip()
except (ValueError, AttributeError):
# If that fails, look for price within list_obj (object "card_info").
try:
price = [n for n in list_obj
if any(["$" in n, "K" in n, "k" in n])]
if len(price) > 0:
price = price[0].split(" ")
price = [n for n in price if re.search("[0-9]", n) is not None]
if len(price[0]) > 0:
price = price[0]
else:
price = "NA"
else:
price = "NA"
except (ValueError, AttributeError):
price = "NA"
if len(price) == 0 or price == "null":
price = "NA"
if price is not "NA":
# Transformations to the price string.
price = price.replace(",", "").replace("+", "").replace("$", "")
if any(["K" in price, "k" in price]):
price = price.lower().split("k")[0].strip()
price = price + "000"
if any(["M" in price, "m" in price]):
price = price.lower().split("m")[0].strip()
if "." not in price:
price = price + "000000"
else:
pricelen = len(price.split('.')[0]) + 6
price = price.replace('.', '')
diff = pricelen - len(price)
price = price + (diff * "0")
if len(price) == 0:
price = 'NA'
return(price)
def get_card_info(soup_obj):
# For most listings, card_info will contain info on number of bedrooms,
# number of bathrooms, square footage, and sometimes price.
try:
card = soup_obj.find(
"span", {"class" : "zsg-photo-card-info"}).get_text().split(" · ")
except (ValueError, AttributeError):
card = "NA"
if len(card) == 0 or card == 'null':
card = "NA"
return(card)
def get_sqft(list_obj):
sqft = [n for n in list_obj if "sqft" in n]
if len(sqft) > 0:
try:
sqft = float(sqft[0].split("sqft")[0].strip().replace(",", "").replace("+", ""))
except (ValueError, IndexError):
sqft = "NA"
if sqft == 0:
sqft = "NA"
else:
sqft = "NA"
return(sqft)
def get_bedrooms(list_obj):
beds = [n for n in list_obj if any(["bd" in n, "tudio" in n])]
if len(beds) > 0:
if any([beds[0] == "Studio", beds[0] == "studio"]):
beds = 0
return(beds)
try:
beds = float(beds[0].split("bd")[0].strip())
except (ValueError, IndexError):
if any([beds[0] == "Studio", beds[0] == "studio"]):
beds = 0
else:
beds = "NA"
else:
beds = "NA"
return(beds)
def get_bathrooms(list_obj):
baths = [n for n in list_obj if "ba" in n]
if len(baths) > 0:
try:
baths = float(baths[0].split("ba")[0].strip())
except (ValueError, IndexError):
baths = "NA"
if baths == 0:
baths = "NA"
else:
baths = "NA"
return(baths)
def get_days_on_market(soup_obj):
try:
dom = soup_obj.find_all(
"span", {"class" : "zsg-photo-card-notification"})
dom = [n for n in dom if "illow" in n.get_text()]
if len(dom) > 0:
dom = dom[0].get_text().strip()
dom = int(dom.split(" ")[0])
else:
dom = "NA"
except (ValueError, AttributeError):
dom = "NA"
return(dom)
def get_sale_type(soup_obj):
try:
saletype = soup_obj.find(
"span", {"class" : "zsg-photo-card-status"}).get_text().strip()
except (ValueError, AttributeError):
saletype = "NA"
if len(saletype) == 0 or saletype == 'null':
saletype = "NA"
return(saletype)
def get_url(soup_obj):
# Try to find url in the BeautifulSoup object.
href = [n["href"] for n in soup_obj.find_all("a", href = True)]
url = [i for i in href if "homedetails" in i]
if len(url) > 0:
url = "http://www.zillow.com/homes/for_sale/" + url[0]
else:
# If that fails, contruct the url from the zpid of the listing.
url = [i for i in href if "zpid" in i and "avorite" not in i]
if len(url) > 0:
zpid = re.findall(r"\d{8,10}", href[0])
if zpid is not None and len(zpid) > 0:
url = 'http://www.zillow.com/homes/for_sale/' \
+ str(zpid[0]) \
+ '_zpid/any_days/globalrelevanceex_sort/29.759534,' \
+ '-95.335321,29.675003,-95.502863_rect/12_zm/'
else:
url = "NA"
else:
url = "NA"
return(url)
def close_connection(driver):
driver.quit()