【问题标题】:Coinmarketcap data nested dictionaryCoinmarketcap 数据嵌套字典
【发布时间】:2019-11-26 18:24:20
【问题描述】:

您好,我仍处于学习 Python 的初级阶段。我正在尝试使用他们的 api 系统从 Coinmarketcap.com 提取数据。我可以使用一本大字典获得输出,但似乎无法弄清楚如何提取特定数据。我想只收到'价格':和'最后更新'。

我尝试引用 .loads 并将数据切片到列表中。我也尝试在字典中建立索引,但输出中的嵌套字典让我难以理解。我看过很多 youtube 教程并在 Google 上寻求帮助,但无法找到解决方案。任何帮助将不胜感激!

import requests

import json


url ='https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest'

api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'

headers = {'Accepts': 'application/json', 'X-CMC_PRO_API_KEY': api_key}

parameters = {'symbol': 'ADA'}

response = requests.get(url, headers = headers, params = parameters)

data = response.json()

data_str = json.dumps(data, indent = 2)

print(data_str)

这是字典的输出:

{"status": {"timestamp": "2019-07-17T20:54:40.829Z", "error_code": 0, "error_message": null, "elapsed": 7, "credit_count": 1} ,“数据”:{“ADA”:{“id”:2010,“名称”:“卡尔达诺”,“符号”:“ADA”,“slug”:“卡尔达诺”,“num_market_pairs”:90,“添加日期” : "2017-10-01T00:00:00.000Z", "tags": ["mineable"], "max_supply": 45000000000, "circulating_supply": 25927070538, "total_supply": 31112483745, "platform": null, "cmc_rank ": 12, "last_updated": "2019-07-17T20:54:04.000Z", "quote": {"USD": {"price": 0.056165857414, "volume_24h": 102375843.427606, "percent_change_1h": -0.816068, “percent_change_24h”:5.42849,“percent_change_7d”:-21.8139,“market_cap”:1456216147.0000284,“last_updated”:“2019-07-17T20:54:04.000Z”}}}}}

【问题讨论】:

    标签: python json dictionary


    【解决方案1】:

    在 Python 中,您可以通过简单地访问字典的值

    value = dict[key]
    

    在您的情况下,您有一个嵌套的 JSON。您可以通过链接键来访问这些值。

    你的 JSON 看起来像

    {
    "status": {
        "timestamp": "2019-07-17T20:54:40.829Z",
        "error_code": 0,
        "error_message": null,
        "elapsed": 7,
        "credit_count": 1
    },
    "data": {
        "ADA": {
            "id": 2010,
            "name": "Cardano",
            "symbol": "ADA",
            "slug": "cardano",
            "num_market_pairs": 90,
            "date_added": "2017-10-01T00:00:00.000Z",
            "tags": ["mineable"],
            "max_supply": 45000000000,
            "circulating_supply": 25927070538,
            "total_supply": 31112483745,
            "platform": null,
            "cmc_rank": 12,
            "last_updated": "2019-07-17T20:54:04.000Z",
            "quote": {
                "USD": {
                    "price": 0.056165857414,
                    "volume_24h": 102375843.427606,
                    "percent_change_1h": -0.816068,
                    "percent_change_24h": 5.42849,
                    "percent_change_7d": -21.8139,
                    "market_cap": 1456216147.0000284,
                    "last_updated": "2019-07-17T20:54:04.000Z"
                }
            }
        }
    }
    }
    

    您可以访问价格为

    price = data['data']['ADA']['quote']['USD']['price']
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      尽管相似,但它不是 Python 内置的字典。是一个 JSON。您可以通过“键”值进行解析。示例:

      import json
      
      a = '{"status": {"timestamp": "2019-07-17T20:54:40.829Z", "error_code": 0, "error_message": null, "elapsed": 7}}'
      b = json.loads(a)
      print(b["status"]["elapsed"])
      

      请注意,一旦您已经在使用请求,您就不必导入 json 模块。 例如:

      requests.get(url).json()[0]["your_target"]) 
      

      分析你得到的响应,也许索引'0'不适用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 2021-02-12
        • 2022-12-05
        • 1970-01-01
        • 2018-01-29
        • 2021-04-18
        相关资源
        最近更新 更多