A
A
Alex Fedorov2021-10-16 10:49:12
Python
Alex Fedorov, 2021-10-16 10:49:12

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


When the first option with success: True goes into the loop and that's it, the loop breaks. Please tell me how to solve this problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-10-16
@robben_55

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 question

Ask a Question

731 491 924 answers to any question