I am trying to build a web scraper for redfin to pull the redfin estimate. I have a function that does this and sometimes it works and sometimes it does not work. I noticed that the reason it does not work is because of the submit function. Sometimes the chrome page wont press the search (submit) button and go to the property page.
I am not sure how to fix this issue and make it more consistent.
Here is my code:
from selenium import webdriver
from selenium.webdriver.remote import webelement
import pandas as pd
import time
from bs4 import BeautifulSoup
driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://www.redfin.com/')
time.sleep(3)
def get_redfin_estimate(address):
search_box = driver.find_element_by_name('searchInputBox')
search_box.send_keys(address)
search_box.submit()
time.sleep(3)
soup = BeautifulSoup(driver.page_source, 'html.parser')
try:
price1 = soup.find('div', {'class', 'avm'}).div.text
return(price1)
except AttributeError:
try:
time.sleep(10)
price2 = soup.find('span',class_='avmLabel').find_next('span', class_='value').text
return(price2)
except:
return('N/A')
print(get_redfin_estimate('687 Catalina Laguna Beach, CA 92651'))
print(get_redfin_estimate('693 Bluebird Canyon Drive, Laguna Beach, CA 92651'))
driver.quit()