2015-04-06 63 views
0

我正在嘗試編寫一個函數,該函數從https://openexchangerates.org/的某個日期收集貨幣數據,並將其作爲一個字符串返回。我有點卡住如何在URL中插入一個日期,它說YYYY-MM-DD ,然後如何把它放到python上。在URL中插入日期並獲取該日期的貨幣數據

任何幫助將非常感激

我的代碼,我到目前爲止如下:

def _fetch_exrates(date): 
    import json 
    import urllib.request 
    f = urllib.request.urlopen('http://openexchangerates.org/api/historical/YYYY-MM-DD.json?app_id=188bd7b8d2634e4cb0602ab5b3c223e7') 
    charset = f.info().get_param('charset', 'utf8') 
    data = f.read() 
    decoded = json.loads(data.decode(charset)) 
    print(json.dumps(decoded, indent=4)) 

import datetime 
print('Please but the year in the form of YYYY and the month as MM and day as DD') 
a = int(input('choose a year :',)) 
b = int(input('choose a month :',)) 
c = int(input('choose a day :',)) 
date = datetime.date(a,b,c) 
print(date) 
+0

將其作爲字符串輸入。 – WannaBeCoder 2015-04-06 11:59:07

回答

0
def _fetch_exrates(year,month,day): 
    import json 
    import urllib.request 
    f = urllib.request.urlopen('http://openexchangerates.org/api/historical/'+year+'-'+month+'-'+day+'.json?app_id=188bd7b8d2634e4cb0602ab5b3c223e7') 
    charset = f.info().get_param('charset', 'utf8') 
    data = f.read() 
    decoded = json.loads(data.decode(charset)) 
    print(json.dumps(decoded, indent=4)) 

print('Please but the year in the form of YYYY and the month as MM and day as DD') 
year = raw_input('choose a year :',) 
month = raw_input('choose a month :',) 
day = raw_input('choose a day :',) 
_fetch_exrates(year,month,day) 

然後使用字符串作爲上述構造它。

+0

對不起,我對python非常陌生,你可以詳細說明我怎麼把它放到網址中,很多謝謝 – PeteG 2015-04-06 12:04:25

+0

你絕對的傳說非常感謝你!欠你很大的時間。 – PeteG 2015-04-06 12:11:49

+0

這並不難。在面對這樣的問題之前,您最好閱讀文檔。 :) – WannaBeCoder 2015-04-06 12:12:54