首页 > 解决方案 > Binance API 获取 Spot 资产余额

问题描述

我试图从我的 Binance 账户中获取我的 Spot 钱包资产的余额。

我试过的:

bal = client.get_account()

print(bal)

回来:

    "makerCommission": 15,
    "takerCommission": 15,
    "buyerCommission": 0,
    "sellerCommission": 0,
    "canTrade": true,
    "canWithdraw": true,
    "canDeposit": true,
    "balances": [
        {
            "asset": "BTC",
            "free": "4723846.89208129",
            "locked": "0.00000000"
        },
        {
            "asset": "LTC",
            "free": "4763368.68006011",
            "locked": "0.00000000"
        }
    ]
}

因此,为了达到我尝试的平衡值:

bal = client.get_account()

for i in bal:
    if(i == 'balances'):
        for e in i:
            print(e)

但这返回了这个:

b
a
l
a
n
c
e
s

那么如何访问我的资产余额呢?

提前致谢。

标签: python-3.xlistdictionarynestedbinance-api-client

解决方案


直接试试get_asset_balance()方法

client.get_asset_balance(asset='BTC')

或尝试从嵌套字典中提取值,bal如下所示:

if "balances" in bal: 
    for b in bal['balances']:
        print(b)

推荐阅读