0

大家好 :) 当谈到 JSON 和 Python 时,我是一个新手,今天正在做一个新项目,非常感谢一些帮助。这就是我到目前为止所拥有的。我的目标是通过几种不同的方式与 Binance API 进行交互。

url = 'https://api.binance.com/'
urlWithSymbol = 'https://api.binance.com/api/v1/trades?symbol='


def getRecentTrades(symbol):
    response = requests.get(urlWithSymbol+symbol+'&limit=10')

    # Print the content of the response (the data the server returned)
    print(response.content.decode("utf-8"))

    data = response.json()
    print(type(data))
    print(data)


getRecentTrades('NPXSBTC')

这里一切正常,只有以字典的形式向我提供响应,我希望能够单独访问“投标”等。你们认为下一步是什么?我是否将数据转换为 JSON 对象?

回复:

{'asks': [['0.00000024', '109846420.00000000', []],
          ['0.00000025', '114178637.00000000', []],
          ['0.00000026', '82322155.00000000', []],
          ['0.00000027', '92902459.00000000', []],
          ['0.00000028', '44228198.00000000', []],
          ['0.00000029', '56824640.00000000', []],
          ['0.00000030', '111613234.00000000', []],
          ['0.00000031', '43773659.00000000', []],
          ['0.00000032', '80669915.00000000', []],
          ['0.00000033', '82725221.00000000', []]],
 'bids': [['0.00000023', '155213182.00000000', []],
          ['0.00000022', '191986504.00000000', []],
          ['0.00000021', '118013185.00000000', []],
          ['0.00000020', '168162758.00000000', []],
          ['0.00000019', '64558205.00000000', []],
          ['0.00000018', '63484191.00000000', []],
          ['0.00000017', '31635740.00000000', []],
          ['0.00000016', '39788788.00000000', []],
          ['0.00000015', '41020041.00000000', []],
          ['0.00000014', '16370913.00000000', []]],
 'lastUpdateId': 5532550}

打印输出:

<class 'list'>
[{'id': 1116367, 'price': '0.00000024', 'qty': '35542.00000000', 'time': 1534169839810, 'isBuyerMaker': False, 'isBestMatch': True}, {'id': 1116368, 'price': '0.00000023', 'qty': '400000.00000000', 'time': 1534169854271, 'isBuyerMaker': True, 'isBestMatch': True}, {'id': 1116369, 'price': '0.00000023', 'qty': '15542.00000000', 'time': 1534169991106, 'isBuyerMaker': True, 'isBestMatch': True}, {'id': 1116370, 'price': '0.00000024', 'qty': '1.00000000', 'time': 1534170015730, 'isBuyerMaker': False, 'isBestMatch': True}, {'id': 1116371, 'price': '0.00000023', 'qty': '19061.00000000', 'time': 1534170017669, 'isBuyerMaker': True, 'isBestMatch': True}, {'id': 1116372, 'price': '0.00000023', 'qty': '39.00000000', 'time': 1534170041722, 'isBuyerMaker': True, 'isBestMatch': True}, {'id': 1116373, 'price': '0.00000024', 'qty': '178943.00000000', 'time': 1534170118065, 'isBuyerMaker': False, 'isBestMatch': True}, {'id': 1116374, 'price': '0.00000023', 'qty': '188.00000000', 'time': 1534170158052, 'isBuyerMaker': True, 'isBestMatch': True}, {'id': 1116375, 'price': '0.00000023', 'qty': '173.00000000', 'time': 1534170160358, 'isBuyerMaker': True, 'isBestMatch': True}, {'id': 1116376, 'price': '0.00000023', 'qty': '32232.00000000', 'time': 1534170293908, 'isBuyerMaker': True, 'isBestMatch': True}]

进程以退出代码 0 结束

4

4 回答 4

3

您可以遍历您的字典以访问每个出价。

前任:

import requests

url = 'https://api.binance.com/'
urlWithSymbol = 'https://api.binance.com/api/v1/depth?symbol='


def getRecentTrades(symbol):
    response = requests.get(urlWithSymbol+symbol)

    data = response.json()
    print(type(data))
    for bid in data["bids"]:
        print(bid)


getRecentTrades('NPXSBTC')

输出:

<type 'dict'>
[u'0.00000023', u'159089575.00000000', []]
[u'0.00000022', u'187598715.00000000', []]
[u'0.00000021', u'118040187.00000000', []]
[u'0.00000020', u'168707413.00000000', []]
[u'0.00000019', u'64558205.00000000', []]
[u'0.00000018', u'63484191.00000000', []]
[u'0.00000017', u'32063443.00000000', []]
[u'0.00000016', u'40413788.00000000', []]
[u'0.00000015', u'41686707.00000000', []]
[u'0.00000014', u'16842512.00000000', []]
[u'0.00000013', u'8228300.00000000', []]
[u'0.00000012', u'3940729.00000000', []]
[u'0.00000011', u'4739318.00000000', []]
[u'0.00000010', u'5012270.00000000', []]
[u'0.00000009', u'15746312.00000000', []]
[u'0.00000008', u'2100806.00000000', []]
[u'0.00000007', u'3053860.00000000', []]
[u'0.00000006', u'13562956.00000000', []]
[u'0.00000005', u'13869819.00000000', []]
[u'0.00000004', u'18357472.00000000', []]
[u'0.00000003', u'76777773.00000000', []]
[u'0.00000002', u'7518568.00000000', []]
[u'0.00000001', u'10500722.00000000', []]
于 2018-08-13T14:27:25.207 回答
1

json 是一种数据交换格式。这似乎更像是一个数据类型操作问题。您是否可以详细说明要从您发布的示例数据中提取的内容?

于 2018-08-13T14:33:13.890 回答
1

你可以简单地迭代你的数据:

for record in data:
    id = record['id']

我可以将你的数据放在 Dataframe 中,例如:

将熊猫导入为 pd

record_list = []
for records in data:
    rocords_list.append(record['id'],record['qty'])

数据 = pd.DataFrame(records_lis,columns=['id','qty'])

现在您需要考虑您需要哪些列,如果需要速度,请使用 Numpy 进行基本操作。

于 2018-08-13T14:37:19.973 回答
0

def getRecentTrades(symbol): response = requests.get(lastTradesUrlWithSymbol+symbol+'&limit=10')

# Print the content of the response (the data the server returned)
print(response.content.decode("utf-8"))

data = response.json()
print(type(data))
print(data)

for record in data:
    id = record['id']
    price = record['price']
    qty = record['qty']

    print(str(id),str(price),str(qty))


getRecentTrades('NPXSBTC')

回复:

1116418 0.00000023 44.00000000
1116419 0.00000023 200000.00000000
1116420 0.00000023 1291335.00000000
1116421 0.00000024 135028.00000000
1116422 0.00000023 147.00000000
1116423 0.00000023 48.00000000
1116424 0.00000024 17887.00000000
1116425 0.00000023 172128.00000000
1116426 0.00000023 15006.00000000
1116427 0.00000023 129.00000000

Process finished with exit code 0
于 2018-08-13T14:49:05.990 回答