erweitert um parameter
This commit is contained in:
parent
1987a41e97
commit
a725f87335
|
|
@ -4,25 +4,27 @@ import argparse
|
||||||
import json
|
import json
|
||||||
from bs4 import BeautifulSoup
|
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.
|
und gibt die darin enthaltene JSON-Struktur als Python-Daten zurück.
|
||||||
"""
|
"""
|
||||||
soup = BeautifulSoup(html, "html.parser")
|
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:
|
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
|
json_str = script.string
|
||||||
data = json.loads(json_str)
|
data = json.loads(json_str)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(
|
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",
|
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",
|
parser.add_argument("-o", "--output", default="extracted.json",
|
||||||
help="Ausgabedatei für das extrahierte JSON (Standard: extracted.json)")
|
help="Ausgabedatei für das extrahierte JSON (Standard: extracted.json)")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
@ -34,7 +36,7 @@ def main():
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = extract_json_from_html(response.text)
|
data = extract_json_from_html(response.text, args.script_id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Fehler beim Extrahieren des JSON:", e)
|
print("Fehler beim Extrahieren des JSON:", e)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue