python3.x - python連oanda的模擬交易api獲取json問題第二問
問題描述
在第一問中已解決連接oanda的模擬交易api獲得EUR_USD的即時(shí)匯率,再次感謝@prolifes的熱情幫助,其程序如下:import requestsimport jsonurl = 'https://api-fxpractice.oanda.com/v1/prices'instruments = ’EUR_USD’account_id = ’cawa11’params = {’instruments’:instruments,’accountId’:account_id}access_token = ’a554db3a48ac8180a6996a5547ba1663-ac5947e64456cc5842a34f4ce05e4380’ headers = {’Authorization’:’Bearer ’+access_token} #Bearer后有空格r = requests.get(url,headers = headers, params=params) print(r.json())所得json為:{’prices’: [{’bid’: 1.0926, ’time’: ’2017-05-03T05:45:25.737018Z’, ’ask’: 1.09274, ’instrument’: ’EUR_USD’}]}現(xiàn)在我想同時(shí)獲得EUR_USD和USD_CAD的即時(shí)匯率,獲得如下形式的json:{’prices’: [{’instrument’: ’EUR_USD’, ’ask’: 1.09324, ’time’: ’2017-05-03T04:44:38.200174Z’, ’bid’: 1.09311},{’instrument’: ’USD_CAD’, ’ask’: 1.37270, ’time’: ’2017-05-03T04:44:38.200174Z’, ’bid’: 1.37251}]}
問題解答
回答1:問題已解決,謝謝各位關(guān)注:import requestsimport json
url = 'https://api-fxpractice.oanda.com/v1/prices'instruments = ’EUR_USD,USD_CAD’account_id = ’cawa11’params = {’instruments’:instruments,’accountId’:account_id}access_token = ’a554db3a48ac8180a6996a5547ba1663-ac5947e64456cc5842a34f4ce05e4380’ headers = {’Connection’: ’Keep-Alive’,’Accept-Encoding’: ’gzip,deflate’,’Authorization’:’Bearer ’+access_token} r = requests.get(url,headers = headers, params=params) price = r.json()print(r.json())print(price’prices’[’instrument’].replace(’_’,’/’),’:’,round((price’prices’[’ask’]+price’prices’[’bid’])/2,4),’ ’,price’prices’[’time’])print(price’prices’[’instrument’].replace(’_’,’/’),’:’,round((price’prices’[’ask’]+price’prices’[’bid’])/2,4),’ ’,price’prices’[’time’])
輸出:{’prices’: [{’bid’: 1.09171, ’ask’: 1.09184, ’instrument’: ’EUR_USD’, ’time’: ’2017-05-03T06:44:19.750556Z’}, {’bid’: 1.37203, ’ask’: 1.37219, ’instrument’: ’USD_CAD’, ’time’: ’2017-05-03T06:44:19.738338Z’}]}EUR/USD : 1.0918 2017-05-03T06:44:19.750556ZUSD/CAD : 1.3721 2017-05-03T06:44:19.738338Z
回答2:這個(gè)已經(jīng)不是技術(shù)層次的問題了,你應(yīng)該去了解一下他的api有沒有提供這個(gè)功能,如果沒有提供同時(shí)獲取兩個(gè)的功能,那你只能分開來取,然后再合并起來
相關(guān)文章:
1. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””2. docker鏡像push報(bào)錯(cuò)3. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問題4. javascript - 在 model里定義的 引用表模型時(shí),model為undefined。5. 利用IPMI遠(yuǎn)程安裝centos報(bào)錯(cuò)!6. node.js - mongodb查找子對(duì)象的名稱為某個(gè)值的對(duì)象的方法7. 運(yùn)行python程序時(shí)出現(xiàn)“應(yīng)用程序發(fā)生異常”的內(nèi)存錯(cuò)誤?8. html5和Flash對(duì)抗是什么情況?9. phpstudy8.1沒集成mysql-front10. 前端 - @media query 使用出現(xiàn)的問題?

網(wǎng)公網(wǎng)安備