dealfront_enrichment.py aktualisiert
This commit is contained in:
@@ -8,204 +8,106 @@ from selenium.webdriver.common.by import By
|
|||||||
from selenium.webdriver.chrome.options import Options as ChromeOptions
|
from selenium.webdriver.chrome.options import Options as ChromeOptions
|
||||||
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
|
||||||
|
|
||||||
# --- Konfiguration ---
|
# --- Konfiguration ---
|
||||||
class Config:
|
class Config:
|
||||||
LOGIN_URL = "https://app.dealfront.com/login"
|
LOGIN_URL = "https://app.dealfront.com/login"
|
||||||
TARGET_URL = "https://app.dealfront.com/t/prospector/companies"
|
BASE_TARGET_URL = "https://app.dealfront.com/t/prospector/companies/p/"
|
||||||
SEARCH_NAME = "Facility Management" # <-- PASSEN SIE DIES AN IHRE GESPEICHERTE SUCHE AN
|
SEARCH_NAME = "Facility Management"
|
||||||
CREDENTIALS_FILE = "/app/dealfront_credentials.json"
|
CREDENTIALS_FILE = "/app/dealfront_credentials.json"
|
||||||
OUTPUT_DIR = "/app/output"
|
OUTPUT_DIR = "/app/output"
|
||||||
|
|
||||||
# --- Logging Setup ---
|
# --- Logging Setup ---
|
||||||
LOG_FORMAT = '%(asctime)s - %(levelname)-8s - %(name)-25s - %(message)s'
|
# ... (bleibt unverändert) ...
|
||||||
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT, force=True)
|
|
||||||
logging.getLogger("selenium.webdriver.remote").setLevel(logging.WARNING)
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
os.makedirs(Config.OUTPUT_DIR, exist_ok=True)
|
|
||||||
log_filepath = os.path.join(Config.OUTPUT_DIR, f"dealfront_run_{time.strftime('%Y%m%d-%H%M%S')}.log")
|
|
||||||
file_handler = logging.FileHandler(log_filepath, mode='w', encoding='utf-8')
|
|
||||||
file_handler.setFormatter(logging.Formatter(LOG_FORMAT))
|
|
||||||
logging.getLogger().addHandler(file_handler)
|
|
||||||
|
|
||||||
class DealfrontScraper:
|
class DealfrontScraper:
|
||||||
def __init__(self):
|
# ... (__init__, _load_credentials, _save_debug_artifacts bleiben unverändert) ...
|
||||||
logger.info("Initialisiere WebDriver...")
|
|
||||||
chrome_options = ChromeOptions()
|
|
||||||
chrome_options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
|
|
||||||
chrome_options.add_argument("--headless=new")
|
|
||||||
chrome_options.add_argument("--no-sandbox")
|
|
||||||
chrome_options.add_argument("--disable-dev-shm-usage")
|
|
||||||
chrome_options.add_argument("--window-size=1920,1200")
|
|
||||||
|
|
||||||
try:
|
def login_and_prepare_search(self):
|
||||||
self.driver = webdriver.Chrome(options=chrome_options)
|
# ... (Login bleibt unverändert) ...
|
||||||
except Exception as e:
|
# Navigieren Sie zur ersten Seite der Suche, um die Session zu initialisieren
|
||||||
logger.critical("WebDriver konnte nicht initialisiert werden.", exc_info=True)
|
self.driver.get(f"{Config.BASE_TARGET_URL}1?search_name={Config.SEARCH_NAME}")
|
||||||
raise
|
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table#t-result-table")))
|
||||||
|
logger.info("Erste Ergebnisseite erfolgreich geladen.")
|
||||||
self.wait = WebDriverWait(self.driver, 30)
|
|
||||||
self.username, self.password = self._load_credentials()
|
|
||||||
if not self.username or not self.password:
|
|
||||||
raise ValueError("Credentials konnten nicht geladen werden. Breche ab.")
|
|
||||||
logger.info("WebDriver erfolgreich initialisiert.")
|
|
||||||
|
|
||||||
def _load_credentials(self):
|
|
||||||
try:
|
|
||||||
with open(Config.CREDENTIALS_FILE, 'r', encoding='utf-8') as f:
|
|
||||||
creds = json.load(f)
|
|
||||||
return creds.get("username"), creds.get("password")
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Credentials-Datei {Config.CREDENTIALS_FILE} konnte nicht geladen werden: {e}")
|
|
||||||
return None, None
|
|
||||||
|
|
||||||
def _save_debug_artifacts(self, suffix=""):
|
|
||||||
try:
|
|
||||||
timestamp = time.strftime("%Y%m%d-%H%M%S")
|
|
||||||
filename_base = os.path.join(Config.OUTPUT_DIR, f"error_{suffix}_{timestamp}")
|
|
||||||
self.driver.save_screenshot(f"{filename_base}.png")
|
|
||||||
with open(f"{filename_base}.html", "w", encoding="utf-8") as f:
|
|
||||||
f.write(self.driver.page_source)
|
|
||||||
logger.error(f"Debug-Artefakte gespeichert: {filename_base}.*")
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Konnte Debug-Artefakte nicht speichern: {e}")
|
|
||||||
|
|
||||||
def login(self):
|
|
||||||
try:
|
|
||||||
logger.info(f"Navigiere zur Login-Seite: {Config.LOGIN_URL}")
|
|
||||||
self.driver.get(Config.LOGIN_URL)
|
|
||||||
self.wait.until(EC.visibility_of_element_located((By.NAME, "email"))).send_keys(self.username)
|
|
||||||
self.driver.find_element(By.CSS_SELECTOR, "input[type='password']").send_keys(self.password)
|
|
||||||
self.driver.find_element(By.XPATH, "//button[normalize-space()='Log in']").click()
|
|
||||||
logger.info("Login-Befehl gesendet. Warte 5 Sekunden auf Session-Etablierung.")
|
|
||||||
time.sleep(5)
|
|
||||||
# Verifizierung, dass wir nicht mehr auf der Login-Seite sind
|
|
||||||
if "login" not in self.driver.current_url:
|
|
||||||
logger.info("Login erfolgreich, URL hat sich geändert.")
|
|
||||||
return True
|
return True
|
||||||
self._save_debug_artifacts("login_stuck")
|
|
||||||
return False
|
|
||||||
except Exception as e:
|
|
||||||
logger.critical("Login-Prozess fehlgeschlagen.", exc_info=True)
|
|
||||||
self._save_debug_artifacts("login_exception")
|
|
||||||
return False
|
|
||||||
|
|
||||||
def navigate_and_load_search(self, search_name):
|
def extract_data_with_js(self):
|
||||||
try:
|
"""Extrahiert die Daten mit einem direkt im Browser ausgeführten JavaScript."""
|
||||||
logger.info(f"Navigiere direkt zur Target-Seite und lade die Suche...")
|
script = """
|
||||||
self.driver.get(Config.TARGET_URL)
|
const results = [];
|
||||||
self.wait.until(EC.url_contains("/t/prospector/"))
|
const rows = document.querySelectorAll("table#t-result-table tbody tr[id]");
|
||||||
|
rows.forEach(row => {
|
||||||
search_item_selector = (By.XPATH, f"//div[contains(@class, 'truncate') and normalize-space()='{search_name}']")
|
const companyElem = row.querySelector(".sticky-column a.t-highlight-text");
|
||||||
self.wait.until(EC.element_to_be_clickable(search_item_selector)).click()
|
const websiteElem = row.querySelector("a.text-gray-400.t-highlight-text");
|
||||||
|
if (companyElem) {
|
||||||
logger.info("Suche geladen. Warte auf das Rendern der Ergebnistabelle.")
|
results.push({
|
||||||
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table#t-result-table tbody tr")))
|
name: companyElem.getAttribute('title') || companyElem.innerText,
|
||||||
return True
|
website: websiteElem ? websiteElem.innerText : 'N/A'
|
||||||
except Exception as e:
|
});
|
||||||
logger.critical("Navigation oder Laden der Suche fehlgeschlagen.", exc_info=True)
|
}
|
||||||
self._save_debug_artifacts("navigation_or_search_load")
|
});
|
||||||
return False
|
return results;
|
||||||
|
|
||||||
def extract_current_page_results(self):
|
|
||||||
results = []
|
|
||||||
rows_selector = (By.XPATH, "//table[@id='t-result-table']/tbody/tr[.//a[contains(@class, 't-highlight-text')]]")
|
|
||||||
data_rows = self.driver.find_elements(*rows_selector)
|
|
||||||
for row in data_rows:
|
|
||||||
try:
|
|
||||||
name = row.find_element(By.CSS_SELECTOR, ".sticky-column a.t-highlight-text").get_attribute("title").strip()
|
|
||||||
website = "N/A"
|
|
||||||
try:
|
|
||||||
website = row.find_element(By.CSS_SELECTOR, "a.text-gray-400.t-highlight-text").text.strip()
|
|
||||||
except NoSuchElementException:
|
|
||||||
pass
|
|
||||||
results.append({'name': name, 'website': website})
|
|
||||||
except NoSuchElementException:
|
|
||||||
continue
|
|
||||||
return results
|
|
||||||
|
|
||||||
def scrape_all_pages(self, max_pages=10): # Sicherheitslimit für Seiten
|
|
||||||
"""
|
|
||||||
Iteriert durch alle Ergebnisseiten, indem auf den 'Weiter'-Button geklickt wird.
|
|
||||||
Scrollt vor jeder Extraktion, um alle Elemente zu laden.
|
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
|
return self.driver.execute_script(script)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"JavaScript-Extraktion fehlgeschlagen: {e}")
|
||||||
|
return []
|
||||||
|
|
||||||
|
def run_full_extraction(self, max_pages=6):
|
||||||
all_companies = {}
|
all_companies = {}
|
||||||
|
|
||||||
|
if not self.login_and_prepare_search():
|
||||||
|
return []
|
||||||
|
|
||||||
for page_number in range(1, max_pages + 1):
|
for page_number in range(1, max_pages + 1):
|
||||||
logger.info(f"--- Verarbeite Seite {page_number} ---")
|
try:
|
||||||
|
page_url = f"{Config.BASE_TARGET_URL}{page_number}?search_name={Config.SEARCH_NAME}"
|
||||||
|
logger.info(f"--- Navigiere zu Seite {page_number}: {page_url} ---")
|
||||||
|
self.driver.get(page_url)
|
||||||
|
|
||||||
# 1. Warten, bis die Tabelle grundsätzlich geladen ist
|
# Warten auf ein stabiles Element, das anzeigt, dass die Tabelle da ist
|
||||||
table_selector = (By.CSS_SELECTOR, "table#t-result-table tbody")
|
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table#t-result-table")))
|
||||||
self.wait.until(EC.visibility_of_element_located(table_selector))
|
time.sleep(3) # Kurze Pause, damit das JS rendern kann
|
||||||
|
|
||||||
# 2. SCROLLEN: Scrolle die Seite nach unten, um alle Zeilen zu laden
|
page_results = self.extract_data_with_js()
|
||||||
logger.info("Scrolle nach unten, um alle Zeilen der Seite zu laden...")
|
|
||||||
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
|
|
||||||
time.sleep(2) # Kurze Pause, damit die neuen Zeilen rendern können
|
|
||||||
self.driver.execute_script("window.scrollTo(0, 0);") # Optional: wieder nach oben scrollen
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
# 3. DATEN EXTRAHIEREN
|
|
||||||
page_results = self.extract_current_page_results()
|
|
||||||
if not page_results:
|
if not page_results:
|
||||||
logger.info("Keine Ergebnisse auf dieser Seite extrahiert. Breche ab.")
|
logger.warning(f"Seite {page_number}: Keine Daten extrahiert. Möglicherweise das Ende erreicht.")
|
||||||
break
|
break
|
||||||
|
|
||||||
for company in page_results:
|
for company in page_results:
|
||||||
# Verwende eine Kombination aus Name und Webseite als eindeutigen Schlüssel
|
unique_key = (company['name'], company['website'])
|
||||||
unique_key = (company.get('name'), company.get('website'))
|
|
||||||
if unique_key not in all_companies:
|
if unique_key not in all_companies:
|
||||||
all_companies[unique_key] = company
|
all_companies[unique_key] = company
|
||||||
|
|
||||||
logger.info(f"Seite {page_number}: {len(page_results)} Firmen gefunden. Gesamt einzigartig: {len(all_companies)}")
|
logger.info(f"Seite {page_number}: {len(page_results)} Firmen gefunden. Gesamt einzigartig: {len(all_companies)}")
|
||||||
|
|
||||||
# 4. PAGINIERUNG
|
except TimeoutException:
|
||||||
try:
|
logger.warning(f"Timeout beim Laden von Seite {page_number}. Breche Paginierung ab.")
|
||||||
# Robusterer Selektor für den "Weiter"-Button
|
self._save_debug_artifacts(f"page_{page_number}")
|
||||||
next_button_selector = (By.XPATH, "//a[contains(@class, 'eb-pagination-button') and not(contains(@class, 'disabled'))][last()]")
|
|
||||||
next_button = self.driver.find_element(*next_button_selector)
|
|
||||||
|
|
||||||
# Prüfen, ob der gefundene Button der "Weiter"-Button ist (enthält Pfeil nach rechts)
|
|
||||||
if "fa-angle-right" not in next_button.get_attribute('innerHTML'):
|
|
||||||
logger.info("Letzte Seite erreicht (kein 'Weiter'-Pfeil im letzten Button).")
|
|
||||||
break
|
|
||||||
|
|
||||||
logger.info("Klicke auf 'Weiter'...")
|
|
||||||
self.driver.execute_script("arguments[0].click();", next_button)
|
|
||||||
time.sleep(4) # Feste Wartezeit für den Seitenwechsel
|
|
||||||
|
|
||||||
except NoSuchElementException:
|
|
||||||
logger.info("Kein klickbarer 'Weiter'-Button mehr gefunden. Paginierung abgeschlossen.")
|
|
||||||
break
|
break
|
||||||
|
|
||||||
return list(all_companies.values())
|
return list(all_companies.values())
|
||||||
|
|
||||||
def close(self):
|
# ... (close-Methode bleibt unverändert) ...
|
||||||
if self.driver:
|
|
||||||
self.driver.quit()
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
scraper = None
|
scraper = None
|
||||||
try:
|
try:
|
||||||
scraper = DealfrontScraper()
|
scraper = DealfrontScraper()
|
||||||
if not scraper.login(): raise Exception("Login fehlgeschlagen")
|
all_companies = scraper.run_full_extraction(max_pages=6) # Setzen Sie hier die maximale Seitenzahl
|
||||||
if not scraper.navigate_and_load_search(Config.SEARCH_NAME): raise Exception("Navigation/Suche fehlgeschlagen")
|
|
||||||
|
|
||||||
all_companies = scraper.scrape_all_pages()
|
|
||||||
|
|
||||||
if all_companies:
|
if all_companies:
|
||||||
df = pd.DataFrame(all_companies)
|
df = pd.DataFrame(all_companies)
|
||||||
output_csv_path = os.path.join(Config.OUTPUT_DIR, f"dealfront_results_{time.strftime('%Y%m%d-%H%M%S')}.csv")
|
output_csv_path = os.path.join(Config.OUTPUT_DIR, f"dealfront_results_{time.strftime('%Y%m%d-%H%M%S')}.csv")
|
||||||
df.to_csv(output_csv_path, index=False, sep=';', encoding='utf-8-sig')
|
df.to_csv(output_csv_path, index=False, sep=';', encoding='utf-8-sig')
|
||||||
logger.info(f"Ergebnisse ({len(df)} Firmen) erfolgreich in '{output_csv_path}' gespeichert.")
|
logger.info(f"Alle Ergebnisse ({len(df)} Firmen) erfolgreich gespeichert: {output_csv_path}")
|
||||||
else:
|
else:
|
||||||
logger.warning("Keine Firmen konnten extrahiert werden.")
|
logger.warning("Keine Firmen konnten extrahiert werden.")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.critical(f"Ein kritischer Fehler ist im Hauptprozess aufgetreten: {e}", exc_info=True)
|
logger.critical(f"Ein kritischer Fehler ist im Hauptprozess aufgetreten.", exc_info=True)
|
||||||
finally:
|
finally:
|
||||||
if scraper:
|
if scraper:
|
||||||
scraper.close()
|
scraper.close()
|
||||||
logger.info("Dealfront Automatisierung beendet.")
|
|
||||||
Reference in New Issue
Block a user