python收集数据直到json文件结束

python收集数据直到json文件结束,python,json,api,Python,Json,Api,我试图从Binance中获得所有硬币的价格,上面的代码运行良好,但问题是,如果他们添加新硬币,我就抓不住它。因此,我考虑对范围内的硬币(0375)更改: binance_prices = {} def get_binance_price(): Prices = "https://api.binance.com/api/v3/ticker/price" r = requests.get(url=Prices) data = r.json() for coin in

我试图从Binance中获得所有硬币的价格,上面的代码运行良好,但问题是,如果他们添加新硬币,我就抓不住它。因此,我考虑对范围内的硬币(0375)更改

binance_prices = {}

def get_binance_price():
    Prices = "https://api.binance.com/api/v3/ticker/price"
    r = requests.get(url=Prices)
    data = r.json()
    for coin in range(0,375):
        binance_prices.update( { data[coin]["symbol"]: data[coin]["price"]} )

但是我如何离开循环呢?

你根本不会这样做。您将遍历数据,而不是任意数字

counter = 0
    while True:
        counter = counter + 1
        binance_prices.update( { data[counter]["symbol"]: data[counter]["price"]} )

Python中的一个重要原则是,您总是直接迭代集合,而不是使用
range
和索引。

我明白了。谢谢
for coin in data:
    binance_prices.update( { coin["symbol"]: coin["price"]} )