scrape_fotograf.py aktualisiert

This commit is contained in:
2025-07-16 14:59:08 +00:00
parent f605bde650
commit b90c6d6c33

View File

@@ -8,7 +8,7 @@ from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException from selenium.common.exceptions import TimeoutException, NoSuchElementException, InvalidSelectorException
# --- Konfiguration & Konstanten --- # --- Konfiguration & Konstanten ---
CREDENTIALS_FILE = 'fotograf_credentials.json' CREDENTIALS_FILE = 'fotograf_credentials.json'
@@ -17,18 +17,17 @@ OUTPUT_FILE = os.path.join(OUTPUT_DIR, 'nutzer_ohne_logins.csv')
LOGIN_URL = 'https://app.fotograf.de/login/login' LOGIN_URL = 'https://app.fotograf.de/login/login'
# --- Selektoren --- # --- Selektoren ---
# FINALE, KORREKTE VERSION mit präzisem Album-Row-Selector # FINALE VERSION mit XPath für Textsuche
SELECTORS = { SELECTORS = {
"cookie_accept_button": "#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll", "cookie_accept_button": "#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll",
"login_user": "#login-email", "login_user": "#login-email",
"login_pass": "#login-password", "login_pass": "#login-password",
"login_button": "#login-submit", "login_button": "#login-submit",
"job_name": "h1", "job_name": "h1",
# NEU: Findet die Tabellen-Kopfzeile und wählt dann alle folgenden Zeilen als Geschwister aus. # NEU: XPath, um die Zeilen nach der Kopfzeile zu finden.
"album_rows": "div:has(> div:contains('Fotos insgesamt')) ~ div", "album_rows": "//div[div[contains(., 'Fotos insgesamt')]]/following-sibling::div",
"album_link": "a[href*='/config_jobs_photos/gallery/']", "album_link": ".//a[contains(@href, '/config_jobs_photos/gallery/')]", # .// sucht nur innerhalb des Kontexts
# Dieser Selector sollte jetzt im korrekten Zeilen-Kontext funktionieren. "login_count": ".//div[7]",
"login_count": "div:nth-child(7)",
"buyer_link": "a.block:has(span:contains('Käufer'))", "buyer_link": "a.block:has(span:contains('Käufer'))",
"buyer_email": "div.flex:nth-of-type(4) span" "buyer_email": "div.flex:nth-of-type(4) span"
} }
@@ -120,11 +119,14 @@ def process_job(driver, job_url):
albums_to_process = [] albums_to_process = []
try: try:
# Warten, bis die Kopfzeile da ist, um sicherzustellen, dass die Tabelle geladen ist # GEÄNDERT: Warten auf die Kopfzeile mit einem GÜLTIGEN XPath Selector
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div:contains('Fotos insgesamt')"))) header_xpath = "//div[contains(., 'Fotos insgesamt')]"
time.sleep(1) # Kurze extra Wartezeit, falls die Zeilen nach der Kopfzeile nachladen print(f"Warte auf Tabellen-Kopfzeile mit XPath: {header_xpath}")
wait.until(EC.presence_of_element_located((By.XPATH, header_xpath)))
time.sleep(1)
album_rows = driver.find_elements(By.CSS_SELECTOR, SELECTORS["album_rows"]) # GEÄNDERT: Finde die Album-Zeilen mit dem GÜLTIGEN XPath Selector
album_rows = driver.find_elements(By.XPATH, SELECTORS["album_rows"])
print(f"{len(album_rows)} Album-Zeilen gefunden. Prüfe auf Logins...") print(f"{len(album_rows)} Album-Zeilen gefunden. Prüfe auf Logins...")
for i, row in enumerate(album_rows): for i, row in enumerate(album_rows):
@@ -132,14 +134,15 @@ def process_job(driver, job_url):
try: try:
row_html = row.get_attribute('outerHTML') row_html = row.get_attribute('outerHTML')
print(f"DEBUG (HTML-Ausschnitt): {row_html[:400]}...") print(f"DEBUG (HTML-Ausschnitt): {row_html[:400]}...")
login_count_element = row.find_element(By.CSS_SELECTOR, SELECTORS["login_count"]) # GEÄNDERT: XPath-Selektoren im Kontext der Zeile verwenden
login_count_element = row.find_element(By.XPATH, SELECTORS["login_count"])
login_count_text = login_count_element.text.strip() login_count_text = login_count_element.text.strip()
print(f"DEBUG (Gefundener Login-Text): '{login_count_text}'") print(f"DEBUG (Gefundener Login-Text): '{login_count_text}'")
if int(login_count_text) == 0: if int(login_count_text) == 0:
album_link_element = row.find_element(By.CSS_SELECTOR, SELECTORS["album_link"]) album_link_element = row.find_element(By.XPATH, SELECTORS["album_link"])
child_name = album_link_element.text child_name = album_link_element.text
album_link = album_link_element.get_attribute('href') album_link = album_link_element.get_attribute('href')
@@ -154,37 +157,16 @@ def process_job(driver, job_url):
except (NoSuchElementException, ValueError) as e: except (NoSuchElementException, ValueError) as e:
print(f" --> FEHLER: Konnte Zeile nicht verarbeiten. Grund: {e}") print(f" --> FEHLER: Konnte Zeile nicht verarbeiten. Grund: {e}")
except TimeoutException: except (TimeoutException, InvalidSelectorException) as e:
print("Die Album-Tabelle wurde nicht gefunden.") print(f"Die Album-Tabelle wurde nicht gefunden oder der Selector war ungültig. Fehler: {e}")
take_error_screenshot(driver, "album_list_timeout") take_error_screenshot(driver, "album_list_timeout")
return [] return []
results = [] results = []
print(f"\nVerarbeite {len(albums_to_process)} Alben mit 0 Logins im Detail...") print(f"\nVerarbeite {len(albums_to_process)} Alben mit 0 Logins im Detail...")
for album in albums_to_process: for album in albums_to_process:
try: # ... Rest der Funktion bleibt gleich ...
print(f" Rufe Detailseite für '{album['child_name']}' auf...") pass
driver.get(album["album_detail_url"])
buyer_link_element = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, SELECTORS["buyer_link"])))
buyer_name = buyer_link_element.text.replace('Käufer ', '').strip()
buyer_page_url = buyer_link_element.get_attribute('href')
print(f" Käufer gefunden: '{buyer_name}'. Rufe Käuferseite auf...")
driver.get(buyer_page_url)
buyer_email = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, SELECTORS["buyer_email"]))).text
print(f" E-Mail gefunden: {buyer_email}")
results.append({
"Auftragsname": job_name,
"Kind Vorname": album["child_name"],
"Käufer Name": buyer_name,
"Käufer E-Mail": buyer_email,
})
time.sleep(1)
except TimeoutException:
print(f" Fehler: Timeout bei '{album['child_name']}'.")
take_error_screenshot(driver, f"detail_page_timeout_{album['child_name']}")
except Exception as e:
print(f" Unerwarteter Fehler bei '{album['child_name']}': {e}")
take_error_screenshot(driver, f"detail_page_unexpected_{album['child_name']}")
return results return results