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:
24
main.py
24
main.py
@@ -8,6 +8,7 @@ import re
|
|||||||
import requests
|
import requests
|
||||||
from selenium import webdriver
|
from selenium import webdriver
|
||||||
from selenium.webdriver.common.by import By
|
from selenium.webdriver.common.by import By
|
||||||
|
from selenium.webdriver.remote.webdriver import WebDriver
|
||||||
|
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
@@ -233,7 +234,7 @@ def notify_webhook(status, last_updated, webhook_url):
|
|||||||
return success
|
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."""
|
"""Versucht höchstens MAX_CAPTCHA_ATTEMPTS-mal, bis eine Statusseite erreicht wird."""
|
||||||
for attempt in range(1, MAX_CAPTCHA_ATTEMPTS + 1):
|
for attempt in range(1, MAX_CAPTCHA_ATTEMPTS + 1):
|
||||||
print(f"[Attempt {attempt}/{MAX_CAPTCHA_ATTEMPTS}] Löse Audio-Captcha …")
|
print(f"[Attempt {attempt}/{MAX_CAPTCHA_ATTEMPTS}] Löse Audio-Captcha …")
|
||||||
@@ -256,11 +257,21 @@ def solve_captcha_flow(driver):
|
|||||||
"Maximale Anzahl an Captcha-Versuchen erreicht, ohne Statusseite zu erhalten."
|
"Maximale Anzahl an Captcha-Versuchen erreicht, ohne Statusseite zu erhalten."
|
||||||
)
|
)
|
||||||
|
|
||||||
if not is_captcha_page(html):
|
if is_captcha_page(html):
|
||||||
raise RuntimeError("Weder Status- noch Captcha-Seite erkannt. Abbruch.")
|
print("Captcha nicht gelöst, versuche es erneut …")
|
||||||
|
driver.get(start_url)
|
||||||
|
|
||||||
print("Captcha nicht gelöst, versuche es erneut …")
|
if error_messages := driver.find_elements(By.CLASS_NAME, "error_message"):
|
||||||
driver.get(start_url)
|
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.")
|
raise RuntimeError("Status konnte nicht ermittelt werden.")
|
||||||
|
|
||||||
@@ -275,7 +286,8 @@ def main():
|
|||||||
if not notify_webhook(status, last_updated, WEBHOOK_URL):
|
if not notify_webhook(status, last_updated, WEBHOOK_URL):
|
||||||
raise RuntimeError("Webhook konnte nicht benachrichtigt werden.")
|
raise RuntimeError("Webhook konnte nicht benachrichtigt werden.")
|
||||||
finally:
|
finally:
|
||||||
driver.quit()
|
# driver.quit()
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user