From a725f873351955be6e9d79edf6a8271301d3162e Mon Sep 17 00:00:00 2001 From: Czechman Date: Sun, 16 Feb 2025 18:08:47 +0100 Subject: [PATCH] erweitert um parameter --- extract_humble_json.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/extract_humble_json.py b/extract_humble_json.py index 109337d..0ef36e6 100644 --- a/extract_humble_json.py +++ b/extract_humble_json.py @@ -4,25 +4,27 @@ import argparse import json from bs4 import BeautifulSoup -def extract_json_from_html(html): +def extract_json_from_html(html, script_id): """ - Sucht in dem HTML nach dem Script-Tag mit ID 'webpack-bundle-page-data' + Sucht in dem HTML nach dem Script-Tag mit der angegebenen ID und gibt die darin enthaltene JSON-Struktur als Python-Daten zurück. """ soup = BeautifulSoup(html, "html.parser") - script = soup.find("script", id="webpack-bundle-page-data", type="application/json") + script = soup.find("script", id=script_id, type="application/json") if script is None: - raise ValueError("Kein passendes Script-Tag mit ID 'webpack-bundle-page-data' gefunden!") + raise ValueError(f"Kein passendes Script-Tag mit ID '{script_id}' gefunden!") json_str = script.string data = json.loads(json_str) return data def main(): parser = argparse.ArgumentParser( - description="Extrahiert JSON aus dem Script-Block von https://www.humblebundle.com/bundles und speichert es in einer Datei." + description="Extrahiert JSON aus einem Script-Block einer Webseite und speichert es in einer Datei." ) parser.add_argument("--url", default="https://www.humblebundle.com/bundles", - help="URL der Bundles-Seite (Standard: https://www.humblebundle.com/bundles)") + help="URL der Seite (Standard: https://www.humblebundle.com/bundles)") + parser.add_argument("--script-id", default="landingPage-json-data", + help="ID des Script-Tags, das das JSON enthält (Standard: landingPage-json-data)") parser.add_argument("-o", "--output", default="extracted.json", help="Ausgabedatei für das extrahierte JSON (Standard: extracted.json)") args = parser.parse_args() @@ -34,7 +36,7 @@ def main(): return try: - data = extract_json_from_html(response.text) + data = extract_json_from_html(response.text, args.script_id) except Exception as e: print("Fehler beim Extrahieren des JSON:", e) return