python3.x - python連oanda的模擬交易api獲取json問題第二問
問題描述
在第一問中已解決連接oanda的模擬交易api獲得EUR_USD的即時匯率,再次感謝@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)在我想同時獲得EUR_USD和USD_CAD的即時匯率,獲得如下形式的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:這個已經(jīng)不是技術(shù)層次的問題了,你應(yīng)該去了解一下他的api有沒有提供這個功能,如果沒有提供同時獲取兩個的功能,那你只能分開來取,然后再合并起來
相關(guān)文章:
1. javascript - immutable配合react提升性能?2. javascript - sublime快鍵鍵問題3. css - 寫頁面遇到個布局問題,求大佬們幫解答,在線等,急!~4. 配置Apache時,添加對PHP的支持時語法錯誤5. phpstudy8.1支持win11系統(tǒng)嗎?6. javascript - nodejs關(guān)于進程間發(fā)送句柄的一點疑問7. javascript - 移動端上不能實現(xiàn)拖拽布局嗎?8. 實現(xiàn)bing搜索工具urlAPI提交9. Apache 已經(jīng)把網(wǎng)站根目錄的改為allow from all了,但是服務(wù)器還是不能訪問?10. vue.js - Vue 如何像Angular.js watch 一樣監(jiān)聽數(shù)據(jù)變化

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