Spaces:
Sleeping
Sleeping
File size: 17,889 Bytes
71601e8 |
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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
# Version: 3
import streamlit as st
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import re
def search_fsbo_address(location):
"""Search FSBO for the given address and return the result page."""
options = Options()
options.add_argument("--incognito")
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("start-maximized")
options.add_argument("--disable-gpu")
options.add_argument("--log-level=3")
options.add_argument(
"user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://fsbo.com/")
try:
search_box = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/main/div[1]/div[6]/div/div/div[4]/form/div/input"))
)
search_box.clear()
search_box.send_keys(location)
time.sleep(2)
search_button = driver.find_element(By.XPATH, "/html/body/div[2]/main/div[1]/div[6]/div/div/div[4]/form/div/div/button")
search_button.click()
time.sleep(5)
return driver
except Exception as e:
st.error(f"Error finding FSBO URL: {e}")
driver.quit()
return None
def clean_text(text):
"""Cleans extracted text by removing Listing ID, unnecessary content, and 'View Listing Details'."""
lines = [line.strip() for line in text.split("\n") if line.strip()]
# Remove Listing ID (e.g., "Listing ID#541799 - ")
lines = [re.sub(r"Listing ID#\d+\s*-", "", line) for line in lines]
# Remove "View Listing Details"
lines = [line for line in lines if "View Listing Details" not in line]
return " | ".join(lines)
def scrape_first_house(driver):
"""Scrape only the first house and return its details properly formatted."""
try:
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/main/div/div[2]/div[1]"))
)
first_listing = driver.find_element(By.XPATH, "/html/body/div[2]/main/div/div[2]/div[1]/div[2]/div[1]/div[1]/div")
details_text = first_listing.get_attribute("innerText").strip()
formatted_details = clean_text(details_text)
return formatted_details
except:
return "N/A"
def scrape_all_houses(driver):
"""Scrape all houses and return a list of details properly formatted."""
houses = []
try:
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/main/div/div[2]/div[1]"))
)
listings = driver.find_elements(By.XPATH, "/html/body/div[2]/main/div/div[2]/div[1]/div[2]/div")
for index, listing in enumerate(listings, start=1):
try:
details_text = listing.get_attribute("innerText").strip()
formatted_details = clean_text(details_text)
houses.append(f"**{index}.** {formatted_details}")
except:
continue
except:
return []
return houses
def main():
st.title("FSBO House Price Finder")
location = st.text_input("Enter Address:")
if st.button("Search"):
with st.spinner("Fetching house details..."):
driver = search_fsbo_address(location)
if not driver:
return
first_house = scrape_first_house(driver)
if first_house != "N/A":
st.success("**First House Found:**")
st.write(first_house)
st.info("Wait... getting all houses in the area")
all_houses = scrape_all_houses(driver)
if all_houses:
st.success("**All Houses in the Area:**")
for house in all_houses:
st.write(house)
else:
st.error("No additional houses found.")
driver.quit()
if __name__ == "__main__":
main()
# version: 2
# import streamlit as st
# import time
# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.chrome.service import Service
# from selenium.webdriver.chrome.options import Options
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
# from webdriver_manager.chrome import ChromeDriverManager
# def search_fsbo_address(location):
# """Search FSBO for the given address and return the result page."""
# options = Options()
# options.add_argument("--incognito")
# options.add_argument("--disable-blink-features=AutomationControlled")
# options.add_argument("start-maximized")
# options.add_argument("--disable-gpu")
# options.add_argument("--log-level=3")
# options.add_argument(
# "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
# )
# driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
# driver.get("https://fsbo.com/")
# try:
# search_box = WebDriverWait(driver, 10).until(
# EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/main/div[1]/div[6]/div/div/div[4]/form/div/input"))
# )
# search_box.clear()
# search_box.send_keys(location)
# time.sleep(2)
# search_button = driver.find_element(By.XPATH, "/html/body/div[2]/main/div[1]/div[6]/div/div/div[4]/form/div/div/button")
# search_button.click()
# time.sleep(5)
# return driver
# except Exception as e:
# st.error(f"Error finding FSBO URL: {e}")
# driver.quit()
# return None
# def format_details(details_text):
# """Formats details by adding '|' after each line."""
# lines = [line.strip() for line in details_text.split("\n") if line.strip()]
# return " | ".join(lines)
# def scrape_first_house(driver):
# """Scrape only the first house and return its details."""
# try:
# WebDriverWait(driver, 10).until(
# EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/main/div/div[2]/div[1]"))
# )
# listing = driver.find_element(By.XPATH, "/html/body/div[2]/main/div/div[2]/div[1]/div[2]/div[1]/div[1]/div")
# details_text = listing.get_attribute("innerText").strip()
# return format_details(details_text)
# except:
# return "N/A"
# def scrape_all_houses(driver):
# """Scrape all houses and return a list of details."""
# houses = []
# try:
# WebDriverWait(driver, 10).until(
# EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/main/div/div[2]/div[1]"))
# )
# listings = driver.find_elements(By.XPATH, "/html/body/div[2]/main/div/div[2]/div[1]/div[2]/div")
# for listing in listings:
# try:
# details_text = listing.get_attribute("innerText").strip()
# formatted_details = format_details(details_text)
# houses.append(formatted_details)
# except:
# continue
# except:
# return []
# return houses
# def main():
# st.title("FSBO House Price Finder")
# location = st.text_input("Enter Address:")
# if st.button("Search"):
# with st.spinner("Fetching house details..."):
# driver = search_fsbo_address(location)
# if not driver:
# return
# first_house = scrape_first_house(driver)
# if first_house != "N/A":
# st.success("First House Found:")
# st.write(first_house)
# st.info("Wait... getting all houses in the area")
# all_houses = scrape_all_houses(driver)
# if all_houses:
# st.success("All Houses in the Area:")
# for house in all_houses:
# st.write(house)
# else:
# st.error("No additional houses found.")
# driver.quit()
# if __name__ == "__main__":
# main()
# Best Version1. it is scrapping and displaying first house details correctly.
# import streamlit as st
# import threading
# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.chrome.service import Service
# from selenium.webdriver.chrome.options import Options
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
# from webdriver_manager.chrome import ChromeDriverManager
# import time
# def search_fsbo_address(location):
# """Search FSBO for the given address and return the first result's URL."""
# options = Options()
# options.add_argument("--incognito")
# options.add_argument("--disable-blink-features=AutomationControlled")
# options.add_argument("start-maximized")
# options.add_argument("--disable-gpu")
# options.add_argument("--log-level=3")
# options.add_argument(
# "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
# )
# driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
# driver.get("https://fsbo.com/")
# try:
# search_box = WebDriverWait(driver, 10).until(
# EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/main/div[1]/div[6]/div/div/div[4]/form/div/input"))
# )
# search_box.clear()
# search_box.send_keys(location)
# time.sleep(2)
# search_button = driver.find_element(By.XPATH, "/html/body/div[2]/main/div[1]/div[6]/div/div/div[4]/form/div/div/button")
# search_button.click()
# time.sleep(5)
# fsbo_url = driver.current_url
# return driver, fsbo_url
# except Exception as e:
# print("Error finding correct FSBO URL:", e)
# driver.quit()
# return None, None
# def format_details(details_text):
# """Formats the details by adding '|' after each line."""
# lines = [line.strip() for line in details_text.split("\n") if line.strip()]
# return " | ".join(lines) # Join lines with '|'
# def scrape_fsbo_details(location):
# """Find the correct FSBO URL and scrape house details."""
# driver, fsbo_url = search_fsbo_address(location)
# if not fsbo_url:
# return {"Details": "N/A", "Link": "N/A"}
# try:
# WebDriverWait(driver, 10).until(
# EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/main/div/div[2]/div[1]"))
# )
# listing = driver.find_element(By.XPATH, "/html/body/div[2]/main/div/div[2]/div[1]/div[2]/div[1]/div[1]/div")
# # Extract raw text
# details_text = listing.get_attribute("innerText").strip()
# # Format details by adding '|'
# formatted_details = format_details(details_text)
# except:
# driver.quit()
# return {"Details": "N/A", "Link": fsbo_url}
# driver.quit()
# return {"Details": formatted_details, "Link": fsbo_url}
# def main():
# st.title("FSBO House Price Finder")
# location = st.text_input("Enter Address:")
# if st.button("Search"):
# with st.spinner("Fetching house details..."):
# house_data = scrape_fsbo_details(location)
# if house_data:
# st.success("House Details:")
# st.write(house_data["Details"]) # Display formatted details
# st.write(f"[View Listing]({house_data['Link']})")
# threading.Thread(target=scrape_fsbo_details, args=(location,), daemon=True).start()
# else:
# st.error("No results found or address not recognized.")
# if __name__ == "__main__":
# main()
## working till searching but not able to scrap the details
# import streamlit as st
# import pandas as pd
# import threading
# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.chrome.service import Service
# from selenium.webdriver.chrome.options import Options
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
# from webdriver_manager.chrome import ChromeDriverManager
# import time
# def search_redfin_address(location):
# """Search Redfin for the given address and return the first result's URL."""
# options = Options()
# # options.add_argument("--headless")
# options.add_argument("--incognito")
# options.add_argument("--disable-blink-features=AutomationControlled")
# options.add_argument("start-maximized")
# options.add_argument(
# "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
# )
# driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
# driver.get("https://fsbo.com/")
# try:
# # Find and enter location into the search box
# search_box = WebDriverWait(driver, 10).until(
# EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/main/div[1]/div[6]/div/div/div[4]/form/div/input"))
# )
# search_box.clear()
# search_box.send_keys(location)
# time.sleep(2)
# # Click the search button
# search_button = driver.find_element(By.XPATH, "/html/body/div[2]/main/div[1]/div[6]/div/div/div[4]/form/div/div/button")
# search_button.click()
# time.sleep(5) # Allow results to load
# # Return updated URL
# redfin_url = driver.current_url
# return driver, redfin_url
# except Exception as e:
# print("Error finding correct Redfin URL:", e)
# driver.quit()
# return None, None
# def scrape_redfin_details(location):
# """Find the correct Redfin URL and scrape house details."""
# driver, redfin_url = search_redfin_address(location)
# if not redfin_url:
# return {"Price": "N/A", "Address": "N/A", "Beds": "N/A", "Baths": "N/A", "Sq Ft": "N/A", "Link": "N/A"}
# try:
# WebDriverWait(driver, 10).until(
# EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/main/div/div[2]/div[1]"))
# )
# listings = driver.find_elements(By.XPATH, "/html/body/div[2]/main/div/div[2]/div[1]/div[2]")
# except:
# driver.quit()
# return {"Price": "N/A", "Address": "N/A", "Beds": "N/A", "Baths": "N/A", "Sq Ft": "N/A", "Link": redfin_url}
# houses = []
# for listing in listings:
# try:
# price = listing.find_element(By.XPATH, ".//div/div/div[3]/div[1]/div[1]/span").text
# except:
# price = "N/A"
# try:
# address = listing.find_element(By.XPATH, ".//div/div/div[3]/div[3]").text
# except:
# address = "N/A"
# try:
# beds = listing.find_element(By.XPATH, ".//div/div/div[3]/div[2]/span[1]").text
# except:
# beds = "N/A"
# try:
# baths = listing.find_element(By.XPATH, ".//div/div/div[3]/div[2]/span[2]").text
# except:
# baths = "N/A"
# try:
# sqft = listing.find_element(By.XPATH, ".//div/div/div[3]/div[2]/span[3]").text
# except:
# sqft = "N/A"
# try:
# link = listing.find_element(By.TAG_NAME, "a").get_attribute("href")
# except:
# link = "N/A"
# houses.append({"Price": price, "Address": address, "Beds": beds, "Baths": baths, "Sq Ft": sqft, "Link": link})
# break # Return only the first house fast
# driver.quit()
# return houses[0]
# def background_scraping(location):
# """Scrapes additional property details in the background."""
# pass
# def main():
# st.title("Redfin House Price Finder")
# location = st.text_input("Enter Address:")
# if st.button("Search"):
# with st.spinner("Fetching house details..."):
# house_data = scrape_redfin_details(location)
# if house_data:
# st.success(f"House Price: {house_data['Price']}")
# st.write(f"**Address:** {house_data['Address']}")
# st.write(f"**Beds:** {house_data['Beds']} | **Baths:** {house_data['Baths']} | **Sq Ft:** {house_data['Sq Ft']}")
# # st.write(f"[View Listing on Redfin]({house_data['Link']})")
# threading.Thread(target=background_scraping, args=(location,), daemon=True).start()
# else:
# st.error("No results found or address not recognized.")
# if __name__ == "__main__":
# main() |