Answer the question
In order to leave comments, you need to log in
Dictionary problem?
Hi all. Parsing one site with prices for goods.
Through the get request I accept a json file with the following data:
first option:
{"success":true}
Second option: {"
success":true,"lowest_price":"£1,321.05"}
Third option:
{"success":true," median_price":"£1,321.05"}
Fourth option:
null
And when a product goes into the cycle that has neither a low price nor an average price (the first option), my code breaks, I'm breaking my head, I just can't figure out how to take it into account in the code . For the second and third code does not break. Here is an excerpt from the code below:
r = requests.get(ITEM_URL.format(item[0]) ) #item[0] название товара вставляется в конец ссылки
r = r.json() #конвертирую это в жсон формат
if r == NULL: #для четвертого вариант в случае если сервер вернет null из-за нагрузки
continue
try:
lowest_p = str(r["lowest_price"])
except KeyError:
if r["success"] != False and r["median_price"] != None:
lowest_p = str(r["median_price"])
elif r["success"] == True and r["lowest_price"] == None and r["median_price"] == None:
continue
else:
continue
except TypeError:
continue
Answer the question
In order to leave comments, you need to log in
if r is None:
continue
price = r.get('lowest_price', r.get('median_price', None))
if price is None:
continue
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question