Warum hat die HTML alle gleiche Klasse und Unterklasse mit unterschiedlichen InformationenPython

Python-Programme
Guest
 Warum hat die HTML alle gleiche Klasse und Unterklasse mit unterschiedlichen Informationen

Post by Guest »

Ich versuche, den Haustyp und die EPC -Bewertung der Website zu verschrotten. "Alle haben den gleichen Klassennamen und den CSS -Selektor < /p>
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 # Ensure you import pandas
import time

# Initialize WebDriver
driver = webdriver.Chrome()

# Open URL
url = "https://www.zoopla.co.uk/house-prices/e ... =list&pn=1"
driver.get(url)

# Wait for the main content to load (adjust time as needed)
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "_17smgnt0"))
)

# Initialize result list to store data
result = []

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

# Extract and print addresses
for house in houses:
try:
item = {
"address": house.find_element(By.XPATH, './/a/h2').text,
"DateLast_sold": house.find_element(By.CSS_SELECTOR, "._1hzil3o9._1hzil3o8._194zg6t7").text,
"Number of Rooms": house.find_element(By.CLASS_NAME, "_1pbf8i53").text,
"EPC Rating": house.find_element(By.CLASS_NAME, "_14bi3x30").text

}

result.append(item) # Append to the result list
except Exception as e:
print(f"Error extracting address or date: {e}")

# Store the result into a dataframe after the loop
df = pd.DataFrame(result)

# Show the result
print(df)

# Close the driver
driver.quit()
< /code>
Hier ist ein Bild der HTML -Datei. Wie kann ich die Bewertung der Freehold und EPC extrahieren, um die richtigen Informationen anzuzeigen.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post