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è)的功能,那你只能分開來(lái)取,然后再合并起來(lái)
相關(guān)文章:
1. mysql - 如何減少使用或者不用LEFT JOIN查詢?2. 視頻文件不能播放,怎么辦?3. mysql - jdbc的問題4. python - 我在使用pip install -r requirements.txt下載時(shí),為什么部分能下載,部分不能下載5. html5 - H5做的手機(jī)分享頁(yè)微信更新后,分享出去不再默認(rèn)顯示第一個(gè)圖 作為縮略圖6. python - 編碼問題求助7. linux - python 抓取公眾號(hào)文章遇到驗(yàn)證問題8. mysql - 分庫(kù)分表、分區(qū)、讀寫分離 這些都是用在什么場(chǎng)景下 ,會(huì)帶來(lái)哪些效率或者其他方面的好處9. node.js - nodejs開發(fā)中常用的連接mysql的庫(kù)10. 網(wǎng)頁(yè)爬蟲 - python 爬取網(wǎng)站 并解析非json內(nèi)容
