Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3f90e2d4f1 | |||
| b238b52fbf |
+78
-43
@@ -26,7 +26,6 @@ from jinja2 import TemplateNotFound
|
||||
import os
|
||||
import sys
|
||||
from bs4 import BeautifulSoup
|
||||
import requests
|
||||
|
||||
# Ensure imports work regardless of whether gunicorn starts in /app or /app/Web.
|
||||
_CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
@@ -9543,70 +9542,106 @@ def _fetch_from_open_library(clean_isbn):
|
||||
|
||||
def _fetch_from_isbn_de(clean_isbn):
|
||||
"""
|
||||
Source 4: isbn.de (Web Scraping für deutsche Schulbücher)
|
||||
Sehr gute Trefferquote für Klett, Cornelsen, Westermann etc.
|
||||
Source 4: isbn.de (Maßgeschneidertes Scraping basierend auf realer HTML-Struktur)
|
||||
Nutzt Open-Graph Meta-Tags und durchsucht die .infotab-Struktur der Sidebar.
|
||||
"""
|
||||
try:
|
||||
# Die URL leitet meist automatisch auf den richtigen Buch-Slug weiter
|
||||
url = f"https://www.isbn.de/buch/{clean_isbn}"
|
||||
|
||||
# Ein User-Agent ist wichtig, da Webseiten simple Python-Scripte oft blockieren
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
||||
}
|
||||
|
||||
response = requests.get(url, headers=headers, timeout=5)
|
||||
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
|
||||
# Prüfen, ob wirklich ein Buch gefunden wurde
|
||||
# (isbn.de wirft manchmal keinen 404, sondern zeigt eine Suchseite ohne Treffer)
|
||||
title_elem = soup.find('h1')
|
||||
if not title_elem or "nicht gefunden" in title_elem.text.lower() or "Suche" in title_elem.text:
|
||||
# 1. Titel aus den Meta-Tags extrahieren (extrem zuverlässig)
|
||||
title_meta = soup.find('meta', property='og:title')
|
||||
title = title_meta.get('content', '').strip() if title_meta else None
|
||||
if not title:
|
||||
h1_elem = soup.find('h1')
|
||||
title = h1_elem.text.strip() if h1_elem else "Unknown Title"
|
||||
|
||||
# Falls wir auf einer Fehler-/Suchseite landen
|
||||
if "nicht gefunden" in title.lower() or "suche" in title.lower():
|
||||
return None
|
||||
|
||||
title = title_elem.text.strip()
|
||||
|
||||
# --- Hilfsfunktion zum Parsen der .infotab-Sidebar-Struktur ---
|
||||
# Jede Zeile dort ist aufgebaut als: <div><div>Label</div>Wert</div>
|
||||
def get_sidebar_value(keyword):
|
||||
infotab = soup.find(class_='infotab')
|
||||
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
|
||||
|
||||
# 2. Verlag holen
|
||||
publisher = get_sidebar_value('verlag')
|
||||
if not publisher:
|
||||
publisher = "Unknown Publisher"
|
||||
|
||||
# 3. Erscheinungsdatum (Aus Meta-Tag oder Sidebar)
|
||||
date_meta = soup.find('meta', property='og:book:release_date')
|
||||
if date_meta and date_meta.get('content'):
|
||||
published_date = date_meta.get('content', '').strip()
|
||||
else:
|
||||
published_date = get_sidebar_value('erschienen am')
|
||||
|
||||
# isbn.de nutzt oft Schema.org Tags (itemprop), was das Auslesen sehr sicher macht
|
||||
authors = "Unknown Author"
|
||||
author_elem = soup.find(itemprop="author")
|
||||
if author_elem:
|
||||
authors = author_elem.text.strip()
|
||||
|
||||
publisher = "Unknown Publisher"
|
||||
publisher_elem = soup.find(itemprop="publisher")
|
||||
if publisher_elem:
|
||||
publisher = publisher_elem.text.strip()
|
||||
|
||||
pub_date = "Unknown Date"
|
||||
date_elem = soup.find(itemprop="datePublished")
|
||||
if date_elem:
|
||||
pub_date = date_elem.text.strip()
|
||||
|
||||
if not published_date:
|
||||
published_date = "Unknown Date"
|
||||
|
||||
# 4. Autor (Schulbücher haben oft keinen Einzelautor, daher kluger Fallback)
|
||||
author_meta = soup.find('meta', property='og:book:author')
|
||||
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:
|
||||
match = re.search(r'\d+', page_count)
|
||||
page_count = match.group(0) if match else "Unknown"
|
||||
else:
|
||||
page_count = "Unknown"
|
||||
|
||||
# 6. Beschreibung aus dem zentralen Textfeld holen und Whitespace bereinigen
|
||||
description = "Keine Beschreibung verfügbar"
|
||||
desc_elem = soup.find(itemprop="description")
|
||||
if desc_elem:
|
||||
description = desc_elem.text.strip()
|
||||
|
||||
desc_div = soup.find(id='bookdesc')
|
||||
if desc_div:
|
||||
# Kombiniert <p> und <ul>-Inhalte zu sauberem Fließtext ohne Zeilenumbruch-Chaos
|
||||
description = " ".join(desc_div.text.split())
|
||||
|
||||
# 7. Cover-Bild (Nutzt den direkten Link zum hochauflösenden Bild aus den Metas)
|
||||
thumbnail = ""
|
||||
img_elem = soup.find('img', itemprop="image")
|
||||
if img_elem and 'src' in img_elem.attrs:
|
||||
thumbnail = img_elem['src']
|
||||
# Falls der Link relativ ist (z.B. /cover/...)
|
||||
if thumbnail.startswith('/'):
|
||||
thumbnail = "https://www.isbn.de" + thumbnail
|
||||
img_meta = soup.find('meta', property='og:image')
|
||||
if img_meta:
|
||||
thumbnail = img_meta.get('content', '').strip()
|
||||
else:
|
||||
img_tag = soup.find('img', id='ISBNcover')
|
||||
if img_tag:
|
||||
thumbnail = img_tag.get('data-big') or img_tag.get('src')
|
||||
|
||||
if thumbnail and thumbnail.startswith('/'):
|
||||
thumbnail = "https://www.isbn.de" + thumbnail
|
||||
|
||||
return {
|
||||
"title": title,
|
||||
"authors": authors,
|
||||
"authors": author,
|
||||
"publisher": publisher,
|
||||
"publishedDate": pub_date,
|
||||
"publishedDate": published_date,
|
||||
"description": description,
|
||||
"pageCount": "Unknown", # Seitenanzahl ist oft nicht standardisiert hinterlegt
|
||||
"price": None,
|
||||
"pageCount": page_count,
|
||||
"price": None,
|
||||
"thumbnail": thumbnail,
|
||||
"source": "isbn-de"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user