Probleme beim nutzen von Regex und Replace

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
OOS4315
User
Beiträge: 3
Registriert: Sonntag 3. Januar 2021, 20:40

Hallo zusammen

Ich bedanke mich schon mal für die Hilfe von euch.

Ich bin gerade dran python zu lernen und wollte paar infos von einer website scrapen.
Und zwar dieser:

https://www.footlocker.de/INTERSHOP/web ... ductDetail

Ich habe mit beautiful soup versucht den Code bis zu meinem nutzen zu minimalisieren.
Dazu habe ich diesen code benutzt:

url = 'https://www.footlocker.de/INTERSHOP/web ... ductDetail'

r = requests.get(url)

soup = BeautifulSoup(r.content, 'html.parser')
containers = soup.find_all('div')[15]

output:
<div 08.01.21="" 09:00&quot;,&quot;sizevalue&quot;:&quot;40&quot;,&quot;quantitywarning&quot;:&quot;&quot;,&quot;quantityoptions&quot;:[],&quot;inventorylevel&quot;:&quot;green&quot;},&quot;314102895704075&quot;:{&quot;quantitymessage&quot;:&quot;dieses="" 09:00&quot;,&quot;sizevalue&quot;:&quot;40.5&quot;,&quot;quantitywarning&quot;:&quot;&quot;,&quot;quantityoptions&quot;:[],&quot;inventorylevel&quot;:&quot;green&quot;},&quot;314102895704080&quot;:{&quot;quantitymessage&quot;:&quot;dieses="" 09:00&quot;,&quot;sizevalue&quot;:&quot;41&quot;,&quot;quantitywarning&quot;:&quot;&quot;,&quot;quantityoptions&quot;:[],&quot;inventorylevel&quot;:&quot;yellow&quot;},&quot;314102895704085&quot;:{&quot;quantitymessage&quot;:&quot;dieses="" 09:00&quot;,&quot;sizevalue&quot;:&quot;42&quot;,&quot;quantitywarning&quot;:&quot;&quot;,&quot;quantityoptions&quot;:[],&quot;inventorylevel&quot;:&quot;green&quot;},&quot;314102895704090&quot;:{&quot;quantitymessage&quot;:&quot;dieses="" 09:00&quot;,&quot;sizevalue&quot;:&quot;42.5&quot;,&quot;quantitywarning&quot;:&quot;&quot;,&quot;quantityoptions&quot;:[],&quot;inventorylevel&quot;:&quot;green&quot;},&quot;314102895704095&quot;:{&quot;quantitymessage&quot;:&quot;dieses="" 09:00&quot;,&quot;sizevalue&quot;:&quot;43&quot;,&quot;quantitywarning&quot;:&quot;&quot;,&quot;quantityoptions&quot;:[],&quot;inventorylevel&quot;:&quot;green&quot;},&quot;314102895704100&quot;:{&quot;quantitymessage&quot;:&quot;dieses="" 09:00&quot;,&quot;sizevalue&quot;:&quot;44&quot;,&quot;quantitywarning&quot;:&quot;&quot;,&quot;quantityoptions&quot;:[],&quot;inventorylevel&quot;:&quot;green&quot;},&quot;314102895704105&quot;:{&quot;quantitymessage&quot;:&quot;dieses="" 09:00&quot;,&quot;sizevalue&quot;:&quot;44.5&quot;,&quot;quantitywarning&quot;:&quot;&quot;,&quot;quantityoptions&quot;:[],&quot;inventorylevel&quot;:&quot;green&quot;},&quot;314102895704110&quot;:{&quot;quantitymessage&quot;:&quot;dieses="" 09:00&quot;,&quot;sizevalue&quot;:&quot;45&quot;,&quot;quantitywarning&quot;:&quot;&quot;,&quot;quantityoptions&quot;:[],&quot;inventorylevel&quot;:&quot;green&quot;},&quot;314102895704120&quot;:{&quot;quantitymessage&quot;:&quot;dieses="" 09:00&quot;,&quot;sizevalue&quot;:&quot;46&quot;,&quot;quantitywarning&quot;:&quot;&quot;,&quot;quantityoptions&quot;:[],&quot;inventorylevel&quot;:&quot;green&quot;},&quot;314102895704130&quot;:{&quot;quantitymessage&quot;:&quot;dieses="" 09:00&quot;,&quot;sizevalue&quot;:&quot;47.5&quot;,&quot;quantitywarning&quot;:&quot;&quot;,&quot;quantityoptions&quot;:[],&quot;inventorylevel&quot;:&quot;green&quot;}}'\ndata-ajaxcontent='\"product-variation-314102895704\"' \ndata-ajaxcontent-source='\"product-variation-314102895704\"' ab="" erh&auml;ltlich="" ist="" produkt="" style="\&quot;display:none;\&quot;\ndata-product-variation-info=\&quot;314102895704\&quot;\ndata-product-variation-info-json='{&quot;314102895704070&quot;:{&quot;quantityMessage&quot;:&quot;Dieses"></div>

Nun wenn man den string "&quot;" mit einem " ersetzen würde sieht man dass das ein json text ist. Deshalb wollte ich diesen string mit einem " ersetzen und den text als json zu lesen und dann weiter zu arbeiten. Nun sobald ich probiere mit regex oder replace dies zu machen gibt er mir errors oder ein leeres [ ] heraus.
Ich habe gemerkt wenn ich am anfang und am ende drei mal diese Zeichen " einsetzte (Beispielt: """ Der gewünschte text """) dann ist es mir möglich mit regex oder replace dies zu ersetzen welches ich aber so im moment nicht machen kann.

Hätte da jemand eine Idee oder kann mir helfen da weiter zu arbeiten?


Vielen Dank!
Benutzeravatar
__blackjack__
User
Beiträge: 14053
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@OOS4315: Bei der URL kommt kein HTML sondern JSON zurück, also ist es schon mal falsch das direkt BeautifulSoup zu übergeben. Hol den Inhalt aus dem JSON und verfütter das an BeautifulSoup, denn das JSON enthält HTML als Wert für den einzigen Schlüssel. Und dann kannst Du das <div> mit dem JSON im Attribut suchen und das an `json.loads()` verfüttern. Kein Grund auf strukturierte Daten in Standardformaten mit so etwas groben wie regulären Ausdrücken oder Zeichenkettenoperationen einzuschlagen.

Code: Alles auswählen

#!/usr/bin/env python3
import json
from pprint import pprint

import bs4
import requests


def main():
    url = (
        "https://www.footlocker.de/INTERSHOP/web/FLE"
        "/Footlocker-Footlocker_DE-Site/de_DE/-/EUR"
        "/ViewProduct-ProductVariationSelect"
    )
    parameters = {
        "BaseSKU": "314102895704",
        "InventoryServerity": "ProductDetail",
    }
    response = requests.get(url, params=parameters)
    response.raise_for_status()
    soup = bs4.BeautifulSoup(response.json()["content"], "html.parser")
    json_attribute_name = "data-product-variation-info-json"
    div_node = soup.find("div", {json_attribute_name: True})
    pprint(json.loads(div_node.get(json_attribute_name)))


if __name__ == "__main__":
    main()
Ausgabe:

Code: Alles auswählen

{'314102895704070': {'inventoryLevel': 'GREEN',
                     'quantityMessage': 'Dieses Produkt ist erhältlich ab '
                                        '08.01.21 09:00',
                     'quantityOptions': [],
                     'quantityWarning': '',
                     'sizeValue': '40'},
 '314102895704075': {'inventoryLevel': 'GREEN',
                     'quantityMessage': 'Dieses Produkt ist erhältlich ab '
                                        '08.01.21 09:00',
                     'quantityOptions': [],
                     'quantityWarning': '',
                     'sizeValue': '40.5'},
 '314102895704080': {'inventoryLevel': 'YELLOW',
                     'quantityMessage': 'Dieses Produkt ist erhältlich ab '
                                        '08.01.21 09:00',
                     'quantityOptions': [],
                     'quantityWarning': '',
                     'sizeValue': '41'},
 '314102895704085': {'inventoryLevel': 'GREEN',
                     'quantityMessage': 'Dieses Produkt ist erhältlich ab '
                                        '08.01.21 09:00',
                     'quantityOptions': [],
                     'quantityWarning': '',
                     'sizeValue': '42'},
 '314102895704090': {'inventoryLevel': 'GREEN',
                     'quantityMessage': 'Dieses Produkt ist erhältlich ab '
                                        '08.01.21 09:00',
                     'quantityOptions': [],
                     'quantityWarning': '',
                     'sizeValue': '42.5'},
 '314102895704095': {'inventoryLevel': 'GREEN',
                     'quantityMessage': 'Dieses Produkt ist erhältlich ab '
                                        '08.01.21 09:00',
                     'quantityOptions': [],
                     'quantityWarning': '',
                     'sizeValue': '43'},
 '314102895704100': {'inventoryLevel': 'GREEN',
                     'quantityMessage': 'Dieses Produkt ist erhältlich ab '
                                        '08.01.21 09:00',
                     'quantityOptions': [],
                     'quantityWarning': '',
                     'sizeValue': '44'},
 '314102895704105': {'inventoryLevel': 'GREEN',
                     'quantityMessage': 'Dieses Produkt ist erhältlich ab '
                                        '08.01.21 09:00',
                     'quantityOptions': [],
                     'quantityWarning': '',
                     'sizeValue': '44.5'},
 '314102895704110': {'inventoryLevel': 'GREEN',
                     'quantityMessage': 'Dieses Produkt ist erhältlich ab '
                                        '08.01.21 09:00',
                     'quantityOptions': [],
                     'quantityWarning': '',
                     'sizeValue': '45'},
 '314102895704120': {'inventoryLevel': 'GREEN',
                     'quantityMessage': 'Dieses Produkt ist erhältlich ab '
                                        '08.01.21 09:00',
                     'quantityOptions': [],
                     'quantityWarning': '',
                     'sizeValue': '46'},
 '314102895704130': {'inventoryLevel': 'GREEN',
                     'quantityMessage': 'Dieses Produkt ist erhältlich ab '
                                        '08.01.21 09:00',
                     'quantityOptions': [],
                     'quantityWarning': '',
                     'sizeValue': '47.5'}}
“Vir, intelligence has nothing to do with politics!” — Londo Mollari
OOS4315
User
Beiträge: 3
Registriert: Sonntag 3. Januar 2021, 20:40

Wow danke dir! Da hab ich mir irgendwas falsch durch den kopf gehen lassen.
Vielen Dank für die Helfe! Bin dir sehr dankbar!
Antworten