简体   繁体   中英

Python getting Data from Coinmarketcap Website API

I would like to only get the "price_usd" value of eg 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)

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM