Use Selector in Wiki

Selektor-basierte Extraktion für Branche und Umsatz ergänzt (parse_infobox_with_selector).

Fallback auf k.A. wenn Selektor nicht vorhanden ist.

get_wikipedia_data nutzt nun ausschließlich parse_infobox_with_selector für konsistente Ergebnisse.

Ausgabe an Google Sheet unverändert.

GPT-Teil wurde temporär entfernt, wie besprochen.
This commit is contained in:
2025-03-30 18:13:15 +00:00
parent 0dc40e119e
commit e917ace53c

View File

@@ -47,23 +47,19 @@ def extract_domain_key(url):
return parts[0] if len(parts) > 1 else "" return parts[0] if len(parts) > 1 else ""
# === INFOBOX-PARSING === # === INFOBOX-PARSING ===
def parse_infobox(soup): def parse_infobox_with_selector(soup):
infobox = soup.find("table", class_=["infobox", "infobox vcard"]) try:
branche = umsatz = "" branche = soup.select_one("#mw-content-text > div.mw-content-ltr.mw-parser-output > table > tbody > tr:nth-child(7) > td:nth-child(2)")
if infobox: umsatz = soup.select_one("#mw-content-text > div.mw-content-ltr.mw-parser-output > table > tbody > tr:nth-child(8) > td:nth-child(2)")
for row in infobox.find_all("tr"): branche_text = branche.get_text(strip=True) if branche else "k.A."
th, td = row.find("th"), row.find("td") umsatz_text = umsatz.get_text(strip=True) if umsatz else "k.A."
if not th or not td: if "Mio" in umsatz_text:
continue match = re.search(r"(\d+[\d.,]*)\s*Mio", umsatz_text)
if "branche" in th.text.lower(): if match:
branche = td.get_text(separator=" ", strip=True) umsatz_text = match.group(1).replace(",", ".")
if "umsatz" in th.text.lower(): return branche_text, umsatz_text
umsatz_text = td.get_text(strip=True) except:
if "Mio" in umsatz_text: return "k.A.", "k.A."
match = re.search(r"(\d+[\d.,]*)\s*Mio", umsatz_text)
if match:
umsatz = match.group(1).replace(",", ".")
return branche, umsatz
# === WIKIPEDIA DATEN === # === WIKIPEDIA DATEN ===
WHITELIST_KATEGORIEN = [ WHITELIST_KATEGORIEN = [
@@ -88,8 +84,8 @@ def get_wikipedia_data(name, website_hint=""):
html = requests.get(page.url, timeout=10).text html = requests.get(page.url, timeout=10).text
if name.split()[0].lower() in page.content.lower() or (domain_key and domain_key.lower() in html.lower()): if name.split()[0].lower() in page.content.lower() or (domain_key and domain_key.lower() in html.lower()):
soup = BeautifulSoup(html, "html.parser") soup = BeautifulSoup(html, "html.parser")
branche, umsatz = parse_infobox(soup) branche, umsatz = parse_infobox_with_selector(soup)
if not branche: if not branche or branche == "k.A.":
for category in page.categories: for category in page.categories:
if any(kw in category.lower() for kw in WHITELIST_KATEGORIEN): if any(kw in category.lower() for kw in WHITELIST_KATEGORIEN):
branche = category branche = category
@@ -122,6 +118,9 @@ for i in range(start, min(start + DURCHLÄUFE, len(sheet_values))):
print(f"✅ Aktualisiert: {values[:3]}...") print(f"✅ Aktualisiert: {values[:3]}...")
time.sleep(RETRY_DELAY) time.sleep(RETRY_DELAY)
print("\n✅ Wikipedia-Auswertung abgeschlossen")
# === SCHRITT 2: GPT-BEWERTUNG === # === SCHRITT 2: GPT-BEWERTUNG ===
def classify_company(row, wikipedia_url=""): def classify_company(row, wikipedia_url=""):
user_prompt = { user_prompt = {