Home > Mobile >  Data api, I need to take the value from buys, only data is output, i check dictionary and nothing
Data api, I need to take the value from buys, only data is output, i check dictionary and nothing

Time:12-17

please help with the api, I put everything in the photos, I need to take the value from buys, but I don't understand how to do it, only data is output

import requests
import json
def get_depth(limit=50):
    response = requests.get(url="...")
    with open("depth.txt", "w") as file:
        file.write(response.text)
data = requests.get("...").text
data = json.loads(data)
for items in data["data"]["buys"]:
    price = items ["price"]
    print("data")
print("data")
def main():
    print(get_depth())
if __name__ == "__main__":
    main()

[my code][https://i.stack.imgur.com/yAQfP.png]
[data][https://i.stack.imgur.com/Po87U.png]
[list][https://i.stack.imgur.com/ObZVM.png]
[result][https://i.stack.imgur.com/lQHIy.png]

CodePudding user response:

You are printing the string "data" instead of the variable itself.

You need to be using print(data)

Anyways to get the price of the buys you need to change your loop like this.

for item in data["data"]["buys"]:
    price = item["price"]
    print(price)

If you notice I removed print(data) because it would flood your screen with the all the data you fetched from the webservice.

  • Related