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’}]}现在我想同时获得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:问题已解决,谢谢各位关注: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:这个已经不是技术层次的问题了,你应该去了解一下他的api有没有提供这个功能,如果没有提供同时获取两个的功能,那你只能分开来取,然后再合并起来
相关文章:
1. javascript - 给页面加了动画,在微信浏览器中显示混乱,急救2. javascript - 一个前端的自我修养3. javascript - 有没有基于angular2的highCharts?4. 如何用html5开发app?ide是netbeans5. javascript - iview 打包之后 找不到自带的icon图片,而且路径重复,点解6. PHP中文网定制款鼠标垫7. python2.7 - 在Eclipse开发Python,怎么去掉控制台输出的其他消息??8. 这个python 程序怎么输出窗口运行9. python - 关于爬虫爬取图片的问题?10. javascript - 新组成的数组打印出来出现问题,里面有对象,但长度为空
