SurvivorX
SurvivorX

Reputation: 23

Python getting Data from Coinmarketcap Website API

I would like to only get the "price_usd" value of e.g. Bitcoin from https://coinmarketcap.com/api/ I've tried this code below, but I can't figure out how to only get this one value.. I am using Python 3.5.3. I'd be glad for every help!

import json 
import requests 

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

Upvotes: 2

Views: 5007

Answers (1)

Cartucho
Cartucho

Reputation: 3319

Try this:

import json 
import requests 

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

You can also use the get method to lookup values from a dictionary. It allows you to provide a default value if the key is missing:

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

Upvotes: 4

Related Questions