Home > front end >  How to get a price from json data, binance api p2p
How to get a price from json data, binance api p2p

Time:08-27

This is my code bids = response.json()["data"]["adv"] NameError: name 'response' is not defined[enter image description here][1] I don't understand how to get to the price, please explain, thank you


headers = {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Content-Length": "123",
    "content-type": "application/json",
    "Host": "p2p.binance.com",
    "Origin": "https://p2p.binance.com",
    "Pragma": "no-cache",
    "TE": "Trailers",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0"
}

data = {
  "asset": "USDT",
  "fiat": "RUB",
  "merchantCheck": False,
  "page": 1,
  "payTypes": ["TinkoffNew"],
  "publisherType": None,
  "rows": 1,
  "tradeType": "SELL"
}

r = requests.post('https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search', headers=headers, json=data)
print(r.text)


def main():

    bids = response.json()["data"]["adv"]

    for item in adv:
        price = item["price"]

    return f"{bids}"


if __name__ == "__main__":
    main()


  [1]: https://i.stack.imgur.com/yk0ML.png

CodePudding user response:

You got few things going here:

  1. You're saving the response to a variable called r, not response, so it is not defined.
  2. You're doing nothing with price (not sure if it's just stayed in your example code or not).
  3. r.json()["data"] returns a list (an array), which means you'll need to choose a numeric index to access adv.

e.g.

import requests

headers = {...

data = {...

r = requests.post(
    'https://p2p.binance.com/bapi/c2c/v2/friendly/c2c/adv/search', headers=headers, json=data)
print(r.text)

def main():
    val = r.json()["data"][0]["adv"] # notice im referring 'r' and im adding the [0] index before ["adv"]
    print(val)

if __name__ == '__main__':
    main()
  • Related