make scraper more robust for "not found" error
All checks were successful
Python tests / tests (push) Successful in 2m22s
All checks were successful
Python tests / tests (push) Successful in 2m22s
This commit is contained in:
22
main.py
22
main.py
@@ -8,6 +8,7 @@ import re
|
||||
import requests
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.common.by import By
|
||||
from selenium.webdriver.remote.webdriver import WebDriver
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
@@ -233,7 +234,7 @@ def notify_webhook(status, last_updated, webhook_url):
|
||||
return success
|
||||
|
||||
|
||||
def solve_captcha_flow(driver):
|
||||
def solve_captcha_flow(driver: WebDriver):
|
||||
"""Versucht höchstens MAX_CAPTCHA_ATTEMPTS-mal, bis eine Statusseite erreicht wird."""
|
||||
for attempt in range(1, MAX_CAPTCHA_ATTEMPTS + 1):
|
||||
print(f"[Attempt {attempt}/{MAX_CAPTCHA_ATTEMPTS}] Löse Audio-Captcha …")
|
||||
@@ -256,12 +257,22 @@ def solve_captcha_flow(driver):
|
||||
"Maximale Anzahl an Captcha-Versuchen erreicht, ohne Statusseite zu erhalten."
|
||||
)
|
||||
|
||||
if not is_captcha_page(html):
|
||||
raise RuntimeError("Weder Status- noch Captcha-Seite erkannt. Abbruch.")
|
||||
|
||||
if is_captcha_page(html):
|
||||
print("Captcha nicht gelöst, versuche es erneut …")
|
||||
driver.get(start_url)
|
||||
|
||||
if error_messages := driver.find_elements(By.CLASS_NAME, "error_message"):
|
||||
errors = [e.text for e in error_messages if e.text.strip(punctuation)]
|
||||
print(f"Es gab folgende Fehler: {errors}")
|
||||
|
||||
if "Das Dokument wurde nicht gefunden." in errors:
|
||||
# Mögliche Gründe; Falsche Dokument-ID, Dokument wurde bereits abgeholt und ist nicht mehr im System, etc.
|
||||
return "Dokument nicht gefunden", datetime.now()
|
||||
else:
|
||||
return "Fehler beim Abrufen des Dokumentenstatus", datetime.now()
|
||||
|
||||
raise RuntimeError("Weder Status- noch Captcha-Seite erkannt. Abbruch.")
|
||||
|
||||
raise RuntimeError("Status konnte nicht ermittelt werden.")
|
||||
|
||||
|
||||
@@ -275,7 +286,8 @@ def main():
|
||||
if not notify_webhook(status, last_updated, WEBHOOK_URL):
|
||||
raise RuntimeError("Webhook konnte nicht benachrichtigt werden.")
|
||||
finally:
|
||||
driver.quit()
|
||||
# driver.quit()
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user