Fixes for the secondary information getting with isbn.de
This commit is contained in:
+64
-62
@@ -9542,8 +9542,8 @@ def _fetch_from_open_library(clean_isbn):
|
|||||||
|
|
||||||
def _fetch_from_isbn_de(clean_isbn):
|
def _fetch_from_isbn_de(clean_isbn):
|
||||||
"""
|
"""
|
||||||
Source 4: isbn.de (Web Scraping für deutsche Schulbücher)
|
Source 4: isbn.de (Maßgeschneidertes Scraping basierend auf realer HTML-Struktur)
|
||||||
Robuste Version mit Fallbacks für Meta-Tags und Tabellendaten.
|
Nutzt Open-Graph Meta-Tags und durchsucht die .infotab-Struktur der Sidebar.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
url = f"https://www.isbn.de/buch/{clean_isbn}"
|
url = f"https://www.isbn.de/buch/{clean_isbn}"
|
||||||
@@ -9552,91 +9552,93 @@ def _fetch_from_isbn_de(clean_isbn):
|
|||||||
}
|
}
|
||||||
|
|
||||||
response = requests.get(url, headers=headers, timeout=5)
|
response = requests.get(url, headers=headers, timeout=5)
|
||||||
|
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
soup = BeautifulSoup(response.text, 'html.parser')
|
soup = BeautifulSoup(response.text, 'html.parser')
|
||||||
|
|
||||||
# 1. Titel prüfen
|
# 1. Titel aus den Meta-Tags extrahieren (extrem zuverlässig)
|
||||||
title_elem = soup.find('h1')
|
title_meta = soup.find('meta', property='og:title')
|
||||||
if not title_elem or "nicht gefunden" in title_elem.text.lower() or "Suche" in title_elem.text:
|
title = title_meta.get('content', '').strip() if title_meta else None
|
||||||
return None
|
if not title:
|
||||||
title = title_elem.text.strip()
|
h1_elem = soup.find('h1')
|
||||||
|
title = h1_elem.text.strip() if h1_elem else "Unknown Title"
|
||||||
|
|
||||||
# --- Hilfsfunktion für unstrukturierte HTML-Tabellen ---
|
# Falls wir auf einer Fehler-/Suchseite landen
|
||||||
def get_detail_by_keyword(keywords):
|
if "nicht gefunden" in title.lower() or "suche" in title.lower():
|
||||||
"""Sucht nach Schlagwörtern (z.B. 'Verlag') und gibt den angrenzenden Wert zurück."""
|
|
||||||
for tag in soup.find_all(['th', 'td', 'strong', 'b', 'span']):
|
|
||||||
text = tag.text.strip().lower()
|
|
||||||
if any(kw in text for kw in keywords):
|
|
||||||
# Wenn es eine Tabellenzelle (th/td) ist, nimm das nächste Geschwister-Element
|
|
||||||
if tag.name in ['th', 'td']:
|
|
||||||
sibling = tag.find_next_sibling('td')
|
|
||||||
if sibling:
|
|
||||||
return sibling.text.strip()
|
|
||||||
# Wenn es ein Label in einem Listen- oder Absatz-Element ist
|
|
||||||
parent = tag.parent
|
|
||||||
if parent and parent.name in ['li', 'p', 'div']:
|
|
||||||
return parent.text.replace(tag.text, '').strip()
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# --- Hilfsfunktion für Schema.org (itemprop) ---
|
# --- Hilfsfunktion zum Parsen der .infotab-Sidebar-Struktur ---
|
||||||
def get_itemprop(prop_name):
|
# Jede Zeile dort ist aufgebaut als: <div><div>Label</div>Wert</div>
|
||||||
elem = soup.find(attrs={"itemprop": prop_name})
|
def get_sidebar_value(keyword):
|
||||||
if elem:
|
infotab = soup.find(class_='infotab')
|
||||||
return elem.get('content') or elem.text.strip()
|
if infotab:
|
||||||
|
for d in infotab.find_all('div'):
|
||||||
|
# Prüfe, ob der Text im inneren Label-Div mit dem Keyword übereinstimmt
|
||||||
|
if d.text.strip().lower() == keyword.lower():
|
||||||
|
parent = d.parent
|
||||||
|
# Ziehe das Label vom Gesamttext ab, um nur den Wert zu erhalten
|
||||||
|
return parent.text.replace(d.text, '', 1).strip()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
authors = get_itemprop("author")
|
# 2. Verlag holen
|
||||||
if not authors:
|
publisher = get_sidebar_value('verlag')
|
||||||
authors = get_detail_by_keyword(['autor', 'herausgeber', 'von:'])
|
|
||||||
authors = authors if authors else "Unknown Author"
|
|
||||||
|
|
||||||
publisher = get_itemprop("publisher")
|
|
||||||
if not publisher:
|
if not publisher:
|
||||||
publisher = get_detail_by_keyword(['verlag'])
|
publisher = "Unknown Publisher"
|
||||||
publisher = publisher if publisher else "Unknown Publisher"
|
|
||||||
|
|
||||||
pub_date = get_itemprop("datePublished")
|
# 3. Erscheinungsdatum (Aus Meta-Tag oder Sidebar)
|
||||||
if not pub_date:
|
date_meta = soup.find('meta', property='og:book:release_date')
|
||||||
pub_date = get_detail_by_keyword(['erscheinungsjahr', 'erschienen', 'datum'])
|
if date_meta and date_meta.get('content'):
|
||||||
pub_date = pub_date if pub_date else "Unknown Date"
|
published_date = date_meta.get('content', '').strip()
|
||||||
|
else:
|
||||||
|
published_date = get_sidebar_value('erschienen am')
|
||||||
|
|
||||||
|
if not published_date:
|
||||||
|
published_date = "Unknown Date"
|
||||||
|
|
||||||
page_count = get_itemprop("numberOfPages")
|
# 4. Autor (Schulbücher haben oft keinen Einzelautor, daher kluger Fallback)
|
||||||
if not page_count:
|
author_meta = soup.find('meta', property='og:book:author')
|
||||||
page_count = get_detail_by_keyword(['seiten', 'umfang'])
|
author = author_meta.get('content', '').strip() if author_meta else ""
|
||||||
|
if not author:
|
||||||
|
author = get_sidebar_value('autor') or get_sidebar_value('herausgeber')
|
||||||
|
|
||||||
|
if not author:
|
||||||
|
# Wenn kein Autor existiert, ist es eine Verlagsredaktion (z.B. "Klett Redaktion")
|
||||||
|
author = f"{publisher} Redaktion" if publisher != "Unknown Publisher" else "Unknown Author"
|
||||||
|
|
||||||
|
# 5. Seitenanzahl
|
||||||
|
page_count = get_sidebar_value('seiten') or get_sidebar_value('umfang')
|
||||||
if page_count:
|
if page_count:
|
||||||
match = re.search(r'\d+', page_count)
|
match = re.search(r'\d+', page_count)
|
||||||
page_count = match.group(0) if match else "Unknown"
|
page_count = match.group(0) if match else "Unknown"
|
||||||
else:
|
else:
|
||||||
page_count = "Unknown"
|
page_count = "Unknown"
|
||||||
|
|
||||||
description = get_itemprop("description")
|
# 6. Beschreibung aus dem zentralen Textfeld holen und Whitespace bereinigen
|
||||||
if not description:
|
description = "Keine Beschreibung verfügbar"
|
||||||
for class_name in ['description', 'zusammenfassung', 'klappentext', 'buch-beschreibung']:
|
desc_div = soup.find(id='bookdesc')
|
||||||
fallback_desc = soup.find('div', class_=re.compile(class_name, re.IGNORECASE))
|
if desc_div:
|
||||||
if fallback_desc:
|
# Kombiniert <p> und <ul>-Inhalte zu sauberem Fließtext ohne Zeilenumbruch-Chaos
|
||||||
description = fallback_desc.text.strip()
|
description = " ".join(desc_div.text.split())
|
||||||
break
|
|
||||||
description = description if description else "Keine Beschreibung verfügbar"
|
|
||||||
|
|
||||||
|
# 7. Cover-Bild (Nutzt den direkten Link zum hochauflösenden Bild aus den Metas)
|
||||||
thumbnail = ""
|
thumbnail = ""
|
||||||
img_elem = soup.find('img', itemprop="image")
|
img_meta = soup.find('meta', property='og:image')
|
||||||
if not img_elem:
|
if img_meta:
|
||||||
img_elem = soup.find('img', class_=lambda c: c and 'cover' in str(c).lower())
|
thumbnail = img_meta.get('content', '').strip()
|
||||||
|
else:
|
||||||
if img_elem and 'src' in img_elem.attrs:
|
img_tag = soup.find('img', id='ISBNcover')
|
||||||
thumbnail = img_elem['src']
|
if img_tag:
|
||||||
if thumbnail.startswith('/'):
|
thumbnail = img_tag.get('data-big') or img_tag.get('src')
|
||||||
thumbnail = "https://www.isbn.de" + thumbnail
|
|
||||||
|
if thumbnail and thumbnail.startswith('/'):
|
||||||
|
thumbnail = "https://www.isbn.de" + thumbnail
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"title": title,
|
"title": title,
|
||||||
"authors": authors,
|
"authors": author,
|
||||||
"publisher": publisher,
|
"publisher": publisher,
|
||||||
"publishedDate": pub_date,
|
"publishedDate": published_date,
|
||||||
"description": description,
|
"description": description,
|
||||||
"pageCount": page_count,
|
"pageCount": page_count,
|
||||||
"price": None,
|
"price": None,
|
||||||
|
|||||||
Reference in New Issue
Block a user