Wiki Debugging

Wikipedia-Validierung über validate_wikipedia_page ergänzt (stellt sicher, dass Seiteninhalt oder Domain zum Firmennamen passen).

Fallback-Parsing parse_infobox_with_fallback() eingebaut für robustere Extraktion von Branche/Umsatz.

Branchenbegriff-Erkennung verbessert (Synonyme wie „Tätigkeitsfeld“, „Industriezweig“ etc.).

Bedingte Auswertung verbessert: Wenn kein Wikipedia-Link → "k.A." für Branche/Umsatz.

Selektor-basierte Extraktion entfernt, da sie fehleranfällig war → durch flexible Infobox-Logik ersetzt.
This commit is contained in:
2025-03-30 18:25:03 +00:00
parent 4038fc1d0e
commit 516221bb4b

View File

@@ -47,19 +47,28 @@ 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_with_selector(soup): def parse_infobox_with_fallback(soup):
try: infobox = soup.find("table", class_="infobox")
branche = soup.select_one("#mw-content-text > div.mw-content-ltr.mw-parser-output > table > tbody > tr:nth-child(7) > td:nth-child(2)") branche = "k.A."
umsatz = soup.select_one("#mw-content-text > div.mw-content-ltr.mw-parser-output > table > tbody > tr:nth-child(8) > td:nth-child(2)") umsatz = "k.A."
branche_text = branche.get_text(strip=True) if branche else "k.A."
umsatz_text = umsatz.get_text(strip=True) if umsatz else "k.A." if infobox:
if "Mio" in umsatz_text: for row in infobox.find_all("tr"):
match = re.search(r"(\d+[\d.,]*)\s*Mio", umsatz_text) th = row.find("th")
if match: td = row.find("td")
umsatz_text = match.group(1).replace(",", ".") if not th or not td:
return branche_text, umsatz_text continue
except: label = th.get_text(strip=True).lower()
return "k.A.", "k.A." value = td.get_text(strip=True)
if any(b in label for b in ["branche", "tätigkeitsfeld", "industriezweig", "wirtschaftszweig"]):
branche = value
if "umsatz" in label and "mio" in value.lower():
match = re.search(r"(\d+[\d.,]*)\s*Mio", value)
if match:
umsatz = match.group(1).replace(",", ".")
return branche, umsatz
# === WIKIPEDIA DATEN === # === WIKIPEDIA DATEN ===
WHITELIST_KATEGORIEN = [ WHITELIST_KATEGORIEN = [
@@ -69,6 +78,13 @@ WHITELIST_KATEGORIEN = [
"logistik", "automobil" "logistik", "automobil"
] ]
def validate_wikipedia_page(content, name, domain_key):
name_fragments = name.lower().split()[:2]
return (
any(frag in content.lower() for frag in name_fragments) or
(domain_key and domain_key.lower() in content.lower())
)
def get_wikipedia_data(name, website_hint=""): def get_wikipedia_data(name, website_hint=""):
domain_key = extract_domain_key(website_hint) domain_key = extract_domain_key(website_hint)
search_terms = [name, domain_key] if domain_key else [name] search_terms = [name, domain_key] if domain_key else [name]
@@ -82,15 +98,16 @@ def get_wikipedia_data(name, website_hint=""):
try: try:
page = wikipedia.page(title, auto_suggest=False) page = wikipedia.page(title, auto_suggest=False)
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 not validate_wikipedia_page(page.content, name, domain_key):
soup = BeautifulSoup(html, "html.parser") continue
branche, umsatz = parse_infobox_with_selector(soup) soup = BeautifulSoup(html, "html.parser")
if not branche or branche == "k.A.": branche, umsatz = parse_infobox_with_fallback(soup)
for category in page.categories: if not branche or branche == "k.A.":
if any(kw in category.lower() for kw in WHITELIST_KATEGORIEN): for category in page.categories:
branche = category if any(kw in category.lower() for kw in WHITELIST_KATEGORIEN):
break branche = category
return page.url, branche or "k.A.", umsatz or "k.A." break
return page.url, branche or "k.A.", umsatz or "k.A."
except: except:
continue continue
except Exception as e: except Exception as e:
@@ -121,6 +138,7 @@ for i in range(start, min(start + DURCHLÄUFE, len(sheet_values))):
print("\n✅ Wikipedia-Auswertung abgeschlossen") 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 = {