So scrollen und klicken Sie mit Selen in Python Booking.com auf "Laden Sie mehr Ergebnisse"?Python

Python-Programme
Anonymous
 So scrollen und klicken Sie mit Selen in Python Booking.com auf "Laden Sie mehr Ergebnisse"?

Post by Anonymous »

Ich bin neu im Web -Scraping mit Selenium und versuche, Immobilienlisten von booking.com zu kratzen. Mein Code (unten) kratzt erfolgreich 25 Ergebnisse, aber ich vermute, dass das Problem darin besteht, dass mehr Ergebnisse verfügbar sind, wenn ich auf die Taste "Weitere Ergebnisse laden" klicken. weit: < /p>
# Relevant imports
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException, NoSuchElementException

# WebDriver setup
driver = webdriver.Chrome(service=Service())
driver.get("https://www.booking.com/searchresults.e ... s=cornwall...")

def handle_no_such_element_exception(data_extraction_task):
try:
return data_extraction_task()
except NoSuchElementException:
return None

items = []

# Load more results logic (This part is where I’m struggling)
while True:
try:
load_more_button = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "[data-testid='load-more-button']"))
)
load_more_button.click()
print("Clicked load more button...")
except (TimeoutException, NoSuchElementException):
print("No more results to load.")
break

# Scraping logic (This part works fine)
property_items = driver.find_elements(By.CSS_SELECTOR, "[data-testid=\"property-card\"]")
for property_item in property_items:
title = handle_no_such_element_exception(lambda: property_item.find_element(By.CSS_SELECTOR, "[data-testid=\"title\"]").text)
address = handle_no_such_element_exception(lambda: property_item.find_element(By.CSS_SELECTOR, "[data-testid=\"address\"]").text)
review_score = handle_no_such_element_exception(lambda: property_item.find_element(By.CSS_SELECTOR, "[data-testid=\"review-score\"]").text)
link = handle_no_such_element_exception(lambda: property_item.find_element(By.CSS_SELECTOR, "[data-testid=\"title-link\"]").get_attribute("href"))
item = {
"title": title,
"address": address,
"review_score": review_score,
"link": link
}
items.append(item)

print(items)
driver.quit()

< /code>
Was ich frage: < /strong> < /p>

Wie kann ich richtig scrollen, um mehr Ergebnisse zu laden?>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post