In [1]:
"""Import potrebnych kniznic
requests na pracu s webom 
pandas na vytvorenie dataframu a pracu s datami
json na pracu s jsonom"""
import requests
import pandas as pd
import json
from pandas.io.json import json_normalize
In [4]:
"""Ulozenie celeho obsahu webu do premennej resp pomocou metody get v kniznici requests."""
resp = requests.get('https://www.rohlik.cz/api/v2/products/')
In [5]:
"""Deserializacia/Dekodovanie jsonu
na python objekty a ulozenie do premennej data pomocou metody loads z
kniznice json."""
import json
data = json.loads(resp.text)
In [6]:
"""Funkcia denormalize, ktora nacita na vstupe
input_value, output_value a prevedie vnorene urovne v JSONe
na jednoduchu formu key:value"""
def denormalize(input_value, output_value):
    input_value = str(input_value)
    output_value = str(output_value)
    for result in data["products"]:
        result[input_value + "_" + output_value] = result[input_value][output_value]

"""Funkcia delete, ktora vymaze vnorenu uroven z JSONu"""
def delete(input_value):
    input_value = str(input_value)
    for result in data["products"]:
        try:
            del result[input_value]
        except:
            pass
In [7]:
"""Vytvorenie docasneho slovnika, kde sa nachadza zoznam vsetkych vnorenych urovni a ich
podurovni"""
temp_dict = {"price" : ["amount", "currency"],
            "price_for_unit" : ["amount", "currency"],
            "brand" : ["name"]}

for i,j in temp_dict.items():
    try:
        for k in j:
            denormalize(i, k)
    except:
        pass

for i in temp_dict:
    delete(i)
In [8]:
"""Dekodovanie Python objektu spet na JSON. Ak je ensure_ascii False, obsah moze obsahovat
aj ne-ASCII znaky - https://terpconnect.umd.edu/~zben/Web/CharSet/htmlchars.html"""
products = json.dumps(data, ensure_ascii = False, indent = 4)
In [9]:
"""Nacitanie JSONu do premennej df pomocou pandas metody read_json"""
df = pd.read_json(products)
In [10]:
#Vypis nadpisov
newDf = json_normalize(data['products'])
In [11]:
newDf.to_csv('products.csv', encoding='utf-8', index=False)
In [2]:
newDf = pd.read_csv('products.csv')
newDf.head(11)
Out[2]:
badge brand_name categories country_of_origin crc32 description id image image_large ingredients ... price_amount price_currency price_for_bottle.amount price_for_bottle.currency price_for_unit_amount price_for_unit_currency primary_category.id unit weight weight_variations
0 NaN Nivea [{'id': 300109000}, {'id': 300109103}, {'id': ... dle aktuální nabídky 4119786657 Nivea Sprej antiperspirant Silver Protect Dyna... 1296725 https://1564005101.rsc.cdn77.org/images/grocer... https://1564005101.rsc.cdn77.org/images/grocer... Butane\nIosbutane\nPropane\nCyclomethicone\nAl... ... 64.9 CZK NaN NaN 432.67 CZK 300109110 l NaN False
1 NaN Nivea [{'id': 300109000}, {'id': 300109103}, {'id': ... dle aktuální nabídky 3851673989 Předchází vzniku bílých stop na černém oblečen... 1296731 https://1564005101.rsc.cdn77.org/images/grocer... https://1564005101.rsc.cdn77.org/images/grocer... Stearyl alcohol\nPPG-14 butyl ether\nCyclometh... ... 64.9 CZK NaN NaN 1622.50 CZK 300109111 kg NaN False
2 NaN Nivea [{'id': 300109000}, {'id': 300109103}, {'id': ... dle aktuální nabídky 3818404418 48 h ochrana\nBez alkoholu a barviv\nDermatolo... 1296737 https://1564005101.rsc.cdn77.org/images/grocer... https://1564005101.rsc.cdn77.org/images/grocer... Butane\nIsobutane\nPropane\nAluminum chlorohyd... ... 64.9 CZK NaN NaN 432.67 CZK 300109105 l NaN False
3 NaN Nivea [{'id': 300109000}, {'id': 300109127}, {'id': ... dle aktuální nabídky 3698401885 Nivea® Intimo Sprchová emulze Sensitive je spe... 1296749 https://1564005101.rsc.cdn77.org/images/grocer... https://1564005101.rsc.cdn77.org/images/grocer... Aqua\nCocamidopropyl betaine\nSodium laureth s... ... 99.9 CZK NaN NaN 399.60 CZK 300109132 l NaN False
4 NaN Nivea [{'id': 300109000}, {'id': 300109060}, {'id': ... dle aktuální nabídky 2384072835 Krémové tekuté mýdlo na ruce s jedinečnou vůní... 1296751 https://1564005101.rsc.cdn77.org/images/grocer... https://1564005101.rsc.cdn77.org/images/grocer... Aqua\nCocamidopropyl betaine\nSodium laureth s... ... 49.9 CZK NaN NaN 199.60 CZK 300109065 l NaN False
5 NaN Parodontax [{'id': 300109000}, {'id': 300109051}, {'id': ... dle aktuální nabídky 1117749507 Zubní kartáček parodontax je speciálně navržen... 1296949 https://1564005101.rsc.cdn77.org/images/grocer... https://1564005101.rsc.cdn77.org/images/grocer... NaN ... 184.9 CZK NaN NaN 61.63 CZK 300109053 ks NaN False
6 NaN Odol [{'id': 75685}, {'id': 75689}, {'id': 134377},... dle aktuální nabídky 2931252705 Trojbarevná zubní pasta Odol3 malé zoubky byla... 1296951 https://1564005101.rsc.cdn77.org/images/grocer... https://1564005101.rsc.cdn77.org/images/grocer... Aqua\nHydrated silica\nSorbitol\nGlycerin\nXan... ... 49.9 CZK NaN NaN 998.00 CZK 300109052 l NaN False
7 NaN Odol [{'id': 300109000}, {'id': 300109051}, {'id': ... dle aktuální nabídky 3510690595 Ústní voda Odol Stoma Paradentol ničí 99 % bak... 1296953 https://1564005101.rsc.cdn77.org/images/grocer... https://1564005101.rsc.cdn77.org/images/grocer... Aqua\nGlycerin\nPEG-60 hydrogenated castor oil... ... 64.9 CZK NaN NaN 129.80 CZK 300109057 l NaN False
8 NaN Racio [{'id': 300101000}, {'id': 300101043}, {'id': ... Česká republika 3026434994 Celozrný chlebíček, který vzniká tzv. "pufován... 1296959 https://1564005101.rsc.cdn77.org/images/grocer... https://1564005101.rsc.cdn77.org/images/grocer... Složení: PŠENICE 60%, rýže 24,5 %, kukuřice 5%... ... 14.9 CZK NaN NaN 114.62 CZK 300101047 kg NaN False
9 NaN Racio [{'id': 300101000}, {'id': 300101043}, {'id': ... Česká republika 167327434 Nadýchané rýžové chlebíčky pro Váš zdravý živo... 1296965 https://1564005101.rsc.cdn77.org/images/grocer... https://1564005101.rsc.cdn77.org/images/grocer... Složení: rýže\n\nAlergeny: Neuvádí se, MŮŽE OB... ... 14.9 CZK NaN NaN 114.62 CZK 300101047 kg NaN False
10 NaN Dobré Pečivo [{'id': 300101000}, {'id': 300101043}, {'id': ... Česká republika 1652126142 \n\nUpozorňujeme, že tento produkt může obsaho... 1296967 https://1564005101.rsc.cdn77.org/images/grocer... https://1564005101.rsc.cdn77.org/images/grocer... Složení: Pšeničná kukuřičná krupice 72%, pšeni... ... 34.9 CZK NaN NaN 89.49 CZK 300101045 kg NaN False

11 rows × 23 columns