【问题标题】:Python getting Data from Coinmarketcap Website APIPython 从 Coinmarketcap 网站 API 获取数据
【发布时间】:2018-05-23 03:49:53
【问题描述】:

我只想获得“price_usd”值,例如来自https://coinmarketcap.com/api/的比特币 我在下面尝试了这段代码,但我不知道如何只获得这个值。我使用的是 Python 3.5.3。我会很高兴得到每一个帮助!

import json 
import requests 

r = requests.get('https://api.coinmarketcap.com/v1/ticker/bitcoin/')
for coin in r.json():
    print(coin)

【问题讨论】:

    标签: python json python-requests


    【解决方案1】:

    试试这个:

    import json 
    import requests 
    
    r = requests.get('https://api.coinmarketcap.com/v1/ticker/bitcoin/')
    for coin in r.json():
        print(coin["price_usd"])
    

    您还可以使用get 方法从字典中查找值。如果缺少键,它允许您提供默认值:

    for coin in r.json():
        print(coin.get("price_usd", "U$S Price not provided"))
    

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 1970-01-01
      • 2019-10-20
      • 2020-08-05
      • 1970-01-01
      • 2020-11-15
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多