[Python] Coinmarketcap data nested dictionary

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to extract data from Coinmarketcap.com using their api system. I am able to get an output with one large dictionary but can't seem to figure out how to pull specific data. I would like to receive just the 'price' and 'last updated'.

    I have tried referencing .loads and slicing the data into list. I have also tried to index in the dictionaries, but the nested dictionaries in the output is making it difficult for me to understand. I have watched many youtube tutorials and googled for help, but am unable to find a solution.

    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)


    Here is the output of the dictionary:


    {"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"}}}}}
    ​

    Continue reading...

Compartilhe esta Página