如何从api响应中提取某些部分

时间:2017-12-13 09:34:22

标签: python api request

我正在学习使用API​​,并且无法从请求中提取某个特定部分。我正在使用代码:

Request('https://api.kucoin.com/v1/open/currencies')

返回此内容:

{"success":true,"code":"OK","msg":"Operation succeeded.","timestamp":1513157553306,"data":{"rates":{"BTC":{"CHF":16406.52,"HRK":105986.66,"MXN":315682.44,"ZAR":225142.48,"INR":1065126.38,"CNY":109471.64,"THB":539369.4,"AUD":21846.03,"ILS":58546.63,"KRW":18012060,"JPY":1877290,"PLN":59102.38,"GBP":12380.19,"IDR":224977080,"HUF":4418661,"PHP":834691.1,"TRY":63315.12,"RUB":974073.68,"HKD":129114.54,"EUR":14057.51,"DKK":104620.46,"USD":16540,"CAD":21224.12,"MYR":67279.75,"BGN":27492.78,"NOK":138107.34,"RON":65134.52,"SGD":22352.15,"CZK":359811.16,"SEK":139630.68,"NZD":23835.79,"BRL":54896.26}},"currencies":[["USD","$"],["EUR","€"],["AUD","$"],["CAD","$"],["CHF","CHF"],["CNY","¥"],["GBP","£"],["JPY","¥"],["NZD","$"],["BGN","лв."],["BRL","R$"],["CZK","Kč"],["DKK","kr"],["HKD","$"],["HRK","kn"],["HUF","Ft"],["IDR","Rp"],["ILS","₪"],["INR","₹"],["KRW","₩"],["MXN","$"],["MYR","RM"],["NOK","kr"],["PHP","₱"],["PLN","zł"],["RON","lei"],["RUB","₽"],["SEK","kr"],["SGD","$"],["THB","฿"],["TRY","₺"],["ZAR","R"]]}}

Process finished with exit code 0

但是我只想从这个回复的一部分中提取价格。我怎么能最好地解决这个问题。

1 个答案:

答案 0 :(得分:3)

响应数据为json,因此您可以使用json模块提取数据

>>> import json
>>> data = 'your response data'
>>> jdata = json.loads(data)
>>> jdata['data']['rates']['BTC']

{'THB': 539369.4,
 'MYR': 67279.75,
 'JPY': 1877290,
 'EUR': 14057.51,
 'MXN': 315682.44,
 'BGN': 27492.78,
 'IDR': 224977080,
 'HKD': 129114.54,
 'NOK': 138107.34,
 'ILS': 58546.63,
 'DKK': 104620.46,
 'RON': 65134.52,
 'RUB': 974073.68,
 'CNY': 109471.64,
 'AUD': 21846.03,
 'SEK': 139630.68,
 'ZAR': 225142.48,
 'CHF': 16406.52,
 'CZK': 359811.16,
 'USD': 16540,
 'GBP': 12380.19,
 'PLN': 59102.38,
 'BRL': 54896.26,
 'INR': 1065126.38,
 'HUF': 4418661,
 'CAD': 21224.12,
 'KRW': 18012060,
 'HRK': 105986.66,
 'NZD': 23835.79,
 'TRY': 63315.12,
 'PHP': 834691.1,
 'SGD': 22352.15}