
基于Python的实时汇率接口调用代码实例.doc
3页基于Python的实时汇率接口调用代码实例代码描述:基于Python的实时汇率接口调用代码实例代码平台:聚合数据#!/usr/bin/python# coding: utf-8import json, urllibfrom urllib import urlencode# #汇率调用示例代码一聚合数据# 接口文档: s/80# def main ():#配置您中请的APPKeyappkey =#1 •常用汇率查询request1(appkey,"GET”)#2 •货币列表request 2(appkeyJ f,GE TH)#3 •实时汇率查询换算r equest3(appk ey「GET")#常用汇率查询def r equestl(appk ey^ m=,,GETfl):url = Hhttp://op< juhe>cn/o nebox/exchan ge/query params = {"key" : appkey, #应用APPKEY(应用详细页查询)}params =urlencode(pa rams)if m ==,IGETU:f = ur llib.urlopen (,,%s?%sf, % (url, params)) else:f = ur llib.urlopen (url, params )conten t= f.read()res = js on・loads(con tent)if res:error_code = res["error_code"]if error_c ode== 0:#成功请求print res["result"]else:pr int "%s:%s”% (res[nerro r_coden]^res ["reason"]) else:print Hr equest api er ror"#货币列表d ef request2(appkeyJ m=nG ET"):url = ,fhttp ://op. juhe.c n/onebox/exc hange/list1'params ={"key" : appkey,#应用APPKEY(应用详细页查询)}params = urlencode(params)if m —GET”:f =urllib.urlop en("%s?%su % (url, param s))else:f =urllib>urlop en(url, para ms)cont f. read ()res =jso n.loads(co ntent)i f res:error_code = res[Herro r_coden]if error _code== 0:#成功请求print re s["resultn]else:print ”%s:%s ” % (res[ner ro^code"]^ es["reason"]) else:print ffrequest api error11#实时汇率查询换算def req uest3(appkey , m=nGETn):url = "http://op. nge/currency ” params = {••from" : 转换汇率前的货币代码••to••: ••••, #转换汇率成的货币代码“key” : app key,#应用APPK EY(应用详细页査询)}par ams= urlenc ode(pa“ams)if m ==nG ETH:f = urllib.u rlopen(,,%s?%sH % (url, params))e Ise:f = urllib.u rlopen(url,params)content = f .readOre s= json.loads(content)if res:error_code= res[ "erro^code"]if e rror_code== 0:#成功请求prin t res[Nresul t”]e Ise:print "%s:%s” % (res [nerror_code H]onH])els e:pr int nrequest api error"if _name_ main ()mai n。
