[Python] Why can't I extract listings information

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to extract the EPC rating from each listings , each time i run my script it it's keeps timing out, what could be the issue ? though i tried increasing the waiting time for the main content to load but I still encounter the same issue. could it be the headless browser not been able to load?

    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 import webdriver
    import pandas as pd
    import time

    # Initialize WebDriver
    options = webdriver.ChromeOptions()
    options.add_argument("--headless") # Run headless
    driver = webdriver.Chrome(options=options)

    # Open main URL
    url = "https://www.zoopla.co.uk/house-prices/england/?new_homes=include&q=england+&orig_q=united+kingdom&view_type=list&pn=1"
    driver.get(url)

    # Wait for the main content to load
    WebDriverWait(driver, 20).until(
    EC.presence_of_element_located((By.CLASS_NAME, "_17smgnt0"))
    )

    # Initialize result list
    result = []

    # Find all house elements
    houses = driver.find_elements(By.CLASS_NAME, "_1hzil3o0")

    # Extract property details
    for house in houses:
    try:
    # Extract listing URL
    listing_url = house.find_element(By.XPATH, './/a').get_attribute("href")

    item = {
    "address": house.find_element(By.XPATH, './/a/h2').text,
    "DateLast_sold": house.find_element(By.CSS_SELECTOR, "._1hzil3o9._1hzil3o8._194zg6t7").text,
    "Property type": house.find_element(By.CSS_SELECTOR, "div._1pbf8i52 p").text,
    "Number of room": house.find_element(By.CSS_SELECTOR, "._1pbf8i51 div:nth-child(2) p").text,
    "Tenure": house.find_element(By.CSS_SELECTOR, ".agepcz0 div:nth-child(1) div").text,
    "Square foot": house.find_element(By.CSS_SELECTOR, ".agepcz0 div:nth-child(2) div").text,
    "Listing URL": listing_url,
    "EPC Rating": "N/A" # Placeholder, will update
    }

    # Open property details in a new tab and scrape EPC
    driver.execute_script("window.open('');") # Open new tab
    driver.switch_to.window(driver.window_handles[1]) # Switch to new tab
    driver.get(listing_url) # Open listing page

    try:
    # Wait for EPC section to load
    epc_element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, '.main-content .z3kgis3 ._1vhryas0 ._8lgu4x1 div:nth-child(3) div'))
    )
    item["EPC Rating"] = epc_element.text # Extract EPC rating

    except Exception:
    print(f"EPC not found for {item['address']}")

    driver.close() # Close new tab
    driver.switch_to.window(driver.window_handles[0]) # Switch back to main tab

    result.append(item) # Append to the result list

    except Exception as e:
    print(f"Error extracting data: {e}")

    # Store the result in a DataFrame
    df = pd.DataFrame(result)

    # Show the result
    print(df)

    # Save to CSV (optional)
    # df.to_csv("zoopla_data.csv", index=False)

    # Close the driver
    driver.quit()

    Continue reading...

Compartilhe esta Página