-
Notifications
You must be signed in to change notification settings - Fork 7
/
Realtor Selenium Search.py
181 lines (151 loc) · 5.81 KB
/
Realtor Selenium Search.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
from selenium import webdriver
import time
import sys
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.add_experimental_option("useAutomationExtension", False);
options.add_experimental_option("excludeSwitches", ["safebrowsing-disable-download-protection", "safebrowsing-disable-auto-update", "disable-client-side-phishing-detection"])
driver = webdriver.Chrome(options=options)
MOurls = ["https://www.realtor.com/realestateandhomes-search/Worth-County_MO/", "https://www.realtor.com/realestateandhomes-search/Webster-County_MO/"]
def catch(e):
exc_type, exc_obj, exc_tb = sys.exc_info()
lineNo = str(exc_tb.tb_lineno)
print('Error : %s : %s at Line %s.' % (type(e), e, lineNo))
def wait_loading():
wait_time = 0
try:
while driver.execute_script('return document.readyState;') != 'complete' and wait_time < 10:
wait_time += 0.1
time.sleep(0.1)
print('Load Complete.')
except Exception as e:
pass
def LoadGoogle():
driver.get('https://www.google.com/')
time.sleep(2)
wait_loading()
def GetElement(driver, elementTag, locator='ID'):
'''Wait for element and then select when it is available'''
try:
if locator == 'ID':
if is_element_present(driver, By.ID, elementTag):
return WebDriverWait(driver, 15).until(
lambda driver: driver.find_element_by_id(elementTag))
else:
print('%s Not Found.' % elementTag)
return None
elif locator == 'NAME':
if is_element_present(driver, By.NAME, elementTag):
return WebDriverWait(driver, 15).until(
lambda driver: driver.find_element_by_name(elementTag))
else:
print('%s Not Found.' % elementTag)
return None
elif locator == 'XPATH':
if is_element_present(driver, By.XPATH, elementTag):
return WebDriverWait(driver, 15).until(
lambda driver: driver.find_element_by_xpath(elementTag))
else:
print('%s Not Found.' % elementTag)
return None
elif locator == 'CSS':
if is_element_present(driver, By.CSS_SELECTOR, elementTag):
return WebDriverWait(driver, 15).until(
lambda driver: driver.find_element_by_css_selector(elementTag))
else:
print('%s Not Found.' % elementTag)
return None
except Exception as e:
catch(e)
return None
def ActionClick(driver, element):
try:
driver.execute_script("arguments[0].scrollIntoView();", element)
webdriver.ActionChains(driver).move_to_element(element).click(element).perform()
except Exception as e:
catch(e)
def is_element_present(driver, how, what):
try:
driver.find_element(by=how, value=what)
except NoSuchElementException:
return False
return True
def WaitTillElementPresent(driver, elementTag, locator='ID'):
'''Wait till element present'''
for i in range(60):
try:
if locator == 'ID':
if is_element_present(driver, By.ID, elementTag):
break
elif locator == 'NAME':
if is_element_present(driver, By.NAME, elementTag):
break
elif locator == 'XPATH':
if is_element_present(driver, By.XPATH, elementTag):
break
elif locator == 'CSS':
if is_element_present(driver, By.CSS_SELECTORS, elementTag):
break
except Exception as e:
print('Exception when WaitTillElementPresent : %s' % e)
pass
time.sleep(0.99)
else:
print("Timed out. Unable to find %s" % elementTag)
def tearDown(driver):
try:
driver.close()
except Exception as e:
pass
try:
driver.quit()
except Exception as e:
catch(e)
pass
def getHomeURLs(urls):
try:
homeurls = list()
for i in range(0, len(urls)):
url = urls[i] + "pg-1"
print(url)
driver.get(url)
wait_loading()
time.sleep(3)
homeurls = getDatailsInPage(driver, homeurls)
driver.delete_all_cookies()
j = 1
while (GetElement(driver,"//a[@title='Go to Next Page']", "XPATH") is not None):
LoadGoogle()
url = urls[i] + "pg-" + str(j+1)
print(url)
driver.get(url)
time.sleep(3)
wait_loading()
homeurls = getDatailsInPage(driver, homeurls)
time.sleep(3)
print(f"{homeurls}")
except Exception as e:
catch(e)
def getDatailsInPage(driver, homeurls):
time.sleep(2)
driver.execute_script("if (document.getElementsByClassName('rdc-global-footer').length >= 1){document.getElementsByClassName('rdc-global-footer')[0].scrollIntoView(false)};")
time.sleep(2)
links = driver.find_elements_by_xpath('//a[@href]')
urlCount = len(links)
print(f"URL count = {urlCount}")
if (urlCount > 100):
for a in links:
if ("realestateandhomes-detail" in a.get_attribute('href')):
homeurls.append(a.get_attribute('href'))
print("Found " + str(len(homeurls)) + " urls in page")
uniqueSet = set(homeurls)
homeurls = list(uniqueSet)
return homeurls
try:
LoadGoogle()
getHomeURLs(MOurls)
except Exception as e:
catch(e)
finally:
tearDown(driver)