From 1987a41e97d735442e2e1e84a5caa123ff9f3b0d Mon Sep 17 00:00:00 2001 From: Czechman Date: Sun, 16 Feb 2025 18:04:20 +0100 Subject: [PATCH] json extrahieren --- extract_humble_json.py | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 extract_humble_json.py diff --git a/extract_humble_json.py b/extract_humble_json.py new file mode 100644 index 0000000..109337d --- /dev/null +++ b/extract_humble_json.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +import requests +import argparse +import json +from bs4 import BeautifulSoup + +def extract_json_from_html(html): + """ + Sucht in dem HTML nach dem Script-Tag mit ID 'webpack-bundle-page-data' + 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") + if script is None: + raise ValueError("Kein passendes Script-Tag mit ID 'webpack-bundle-page-data' 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." + ) + parser.add_argument("--url", default="https://www.humblebundle.com/bundles", + help="URL der Bundles-Seite (Standard: https://www.humblebundle.com/bundles)") + parser.add_argument("-o", "--output", default="extracted.json", + help="Ausgabedatei für das extrahierte JSON (Standard: extracted.json)") + args = parser.parse_args() + + # HTTP-Request an die URL + response = requests.get(args.url) + if response.status_code != 200: + print(f"Fehler beim Laden der URL {args.url} (Status-Code: {response.status_code}).") + return + + try: + data = extract_json_from_html(response.text) + except Exception as e: + print("Fehler beim Extrahieren des JSON:", e) + return + + # JSON in eine Datei schreiben + with open(args.output, "w", encoding="utf-8") as f: + json.dump(data, f, indent=2, ensure_ascii=False) + print(f"JSON erfolgreich extrahiert und in '{args.output}' gespeichert.") + +if __name__ == "__main__": + main()