簡體   English   中英

如何從 json 數據中獲取價格,幣安 api p2p

[英]How to get a price from json data, binance api p2p

這是我的代碼 bids = response.json()["data"]["adv"] NameError: name 'response' is not defined[enter image description here][1] 我不明白如何獲得價格,請解釋一下,謝謝


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

你有幾件事要發生在這里:

  1. 您將響應保存到名為r的變量,而不是response ,因此未定義。
  2. 您對price沒有做任何事情(不確定它是否只是留在您的示例代碼中)。
  3. r.json()["data"]返回一個列表(一個數組),這意味着您需要選擇一個數字索引來訪問adv

例如

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM