Python的净值数据接口调用示例分享


Posted in Python onMarch 15, 2016

代码描述:基于Python的净值数据接口调用代码实例
关联数据:净值数据
接口地址:https://www.juhe.cn/docs/api/id/25

#!/usr/bin/python
# -*- coding: utf-8 -*-
import json, urllib
from urllib import urlencode

#----------------------------------
# 净值数据调用示例代码 - 聚合数据
# 在线接口文档:http://www.juhe.cn/docs/25
#----------------------------------

def main():

  #配置您申请的APPKey
  appkey = "*********************"

  #1.全部开放基金
  request1(appkey,"GET")

  #2.股票型基金
  request2(appkey,"GET")

  #3.普通债券型基金
  request3(appkey,"GET")

  #4.货币型基金
  request4(appkey,"GET")

  #5.封闭型基金
  request5(appkey,"GET")
 
  #6.创新封基
  request6(appkey,"GET")

  #7.LOF
  request7(appkey,"GET")

  #8.ETF
  request8(appkey,"GET")

  #9.QDII
  request9(appkey,"GET")

#全部开放基金
def request1(appkey, m="GET"):
  url = "http://web.juhe.cn:8080/fund/netdata/all"
  params = {
    "key" : appkey, #APPKEY值

  }
  params = urlencode(params)
  if m =="GET":
    f = urllib.urlopen("%s?%s" % (url, params))
  else:
    f = urllib.urlopen(url, params)
 
  content = f.read()
  res = json.loads(content)
  if res:
    error_code = res["error_code"]
    if error_code == 0:
      #成功请求
      print res["result"]
    else:
      print "%s:%s" % (res["error_code"],res["reason"])
  else:
    print "request api error"
 
#股票型基金
def request2(appkey, m="GET"):
  url = "http://web.juhe.cn:8080/fund/netdata/stock"
  params = {
    "key" : appkey, #APPKEY值
 
  }
  params = urlencode(params)
  if m =="GET":
    f = urllib.urlopen("%s?%s" % (url, params))
  else:
    f = urllib.urlopen(url, params)
 
  content = f.read()
  res = json.loads(content)
  if res:
    error_code = res["error_code"]
    if error_code == 0:
      #成功请求
      print res["result"]
    else:
      print "%s:%s" % (res["error_code"],res["reason"])
  else:
    print "request api error"
 
#普通债券型基金
def request3(appkey, m="GET"):
  url = "http://web.juhe.cn:8080/fund/netdata/bond"
  params = {
    "key" : appkey, #APPKEY值
 
  }
  params = urlencode(params)
  if m =="GET":
    f = urllib.urlopen("%s?%s" % (url, params))
  else:
    f = urllib.urlopen(url, params)
 
  content = f.read()
  res = json.loads(content)
  if res:
    error_code = res["error_code"]
    if error_code == 0:
      #成功请求
      print res["result"]
    else:
      print "%s:%s" % (res["error_code"],res["reason"])
  else:
    print "request api error"
 
#货币型基金
def request4(appkey, m="GET"):
  url = "http://web.juhe.cn:8080/fund/netdata/monet"
  params = {
    "key" : appkey, #APPKEY值
 
  }
  params = urlencode(params)
  if m =="GET":
    f = urllib.urlopen("%s?%s" % (url, params))
  else:
    f = urllib.urlopen(url, params)
 
  content = f.read()
  res = json.loads(content)
  if res:
    error_code = res["error_code"]
    if error_code == 0:
      #成功请求
      print res["result"]
    else:
      print "%s:%s" % (res["error_code"],res["reason"])
  else:
    print "request api error"
 
#封闭型基金
def request5(appkey, m="GET"):
  url = "http://web.juhe.cn:8080/fund/netdata/close"
  params = {
    "key" : appkey, #APPKEY值
 
  }
  params = urlencode(params)
  if m =="GET":
    f = urllib.urlopen("%s?%s" % (url, params))
  else:
    f = urllib.urlopen(url, params)
 
  content = f.read()
  res = json.loads(content)
  if res:
    error_code = res["error_code"]
    if error_code == 0:
      #成功请求
      print res["result"]
    else:
      print "%s:%s" % (res["error_code"],res["reason"])
  else:
    print "request api error"
 
#创新封基
def request6(appkey, m="GET"):
  url = "http://web.juhe.cn:8080/fund/netdata/innov"
  params = {
    "key" : appkey, #APPKEY值
 
  }
  params = urlencode(params)
  if m =="GET":
    f = urllib.urlopen("%s?%s" % (url, params))
  else:
    f = urllib.urlopen(url, params)
 
  content = f.read()
  res = json.loads(content)
  if res:
    error_code = res["error_code"]
    if error_code == 0:
      #成功请求
      print res["result"]
    else:
      print "%s:%s" % (res["error_code"],res["reason"])
  else:
    print "request api error"
 
#LOF
def request7(appkey, m="GET"):
  url = "http://web.juhe.cn:8080/fund/netdata/lof"
  params = {
    "key" : appkey, #APPKEY值
 
  }
  params = urlencode(params)
  if m =="GET":
    f = urllib.urlopen("%s?%s" % (url, params))
  else:
    f = urllib.urlopen(url, params)
 
  content = f.read()
  res = json.loads(content)
  if res:
    error_code = res["error_code"]
    if error_code == 0:
      #成功请求
      print res["result"]
    else:
      print "%s:%s" % (res["error_code"],res["reason"])
  else:
    print "request api error"
 
#ETF
def request8(appkey, m="GET"):
  url = "http://web.juhe.cn:8080/fund/netdata/etf"
  params = {
    "key" : appkey, #APPKEY值
 
  }
  params = urlencode(params)
  if m =="GET":
    f = urllib.urlopen("%s?%s" % (url, params))
  else:
    f = urllib.urlopen(url, params)
 
  content = f.read()
  res = json.loads(content)
  if res:
    error_code = res["error_code"]
    if error_code == 0:
      #成功请求
      print res["result"]
    else:
      print "%s:%s" % (res["error_code"],res["reason"])
  else:
    print "request api error"
 
#QDII
def request9(appkey, m="GET"):
  url = "http://web.juhe.cn:8080/fund/netdata/qdii"
  params = {
    "key" : appkey, #APPKEY值
 
  }
  params = urlencode(params)
  if m =="GET":
    f = urllib.urlopen("%s?%s" % (url, params))
  else:
    f = urllib.urlopen(url, params)
 
  content = f.read()
  res = json.loads(content)
  if res:
    error_code = res["error_code"]
    if error_code == 0:
      #成功请求
      print res["result"]
    else:
      print "%s:%s" % (res["error_code"],res["reason"])
  else:
    print "request api error"

if __name__ == '__main__':
  main()
Python 相关文章推荐
python 将数据保存为excel的xls格式(实例讲解)
May 03 Python
python判断列表的连续数字范围并分块的方法
Nov 16 Python
Django之Mode的外键自关联和引用未定义的Model方法
Dec 15 Python
python3 http提交json参数并获取返回值的方法
Dec 19 Python
Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str
Aug 07 Python
Python3操作Excel文件(读写)的简单实例
Sep 02 Python
Pytorch之Variable的用法
Dec 31 Python
Python+OpenCV实现旋转文本校正方式
Jan 09 Python
TensorFlow绘制loss/accuracy曲线的实例
Jan 21 Python
python3实现往mysql中插入datetime类型的数据
Mar 02 Python
Python urlopen()参数代码示例解析
Dec 10 Python
用python画城市轮播地图
May 28 Python
Python简单连接MongoDB数据库的方法
Mar 15 #Python
Python函数中的函数(闭包)用法实例
Mar 15 #Python
实例讲解Python中函数的调用与定义
Mar 14 #Python
Python使用multiprocessing实现一个最简单的分布式作业调度系统
Mar 14 #Python
简单讲解Python中的字符串与字符串的输入输出
Mar 13 #Python
深入解析Python中的list列表及其切片和迭代操作
Mar 13 #Python
Python中的列表生成式与生成器学习教程
Mar 13 #Python
You might like
解决PHP超大文件下载,断点续传下载的方法详解
2013/06/06 PHP
WordPress中Gravatar头像缓存到本地及相关优化的技巧
2015/12/19 PHP
PHP入门教程之数学运算技巧总结
2016/09/11 PHP
laravel 解决多库下的DB::transaction()事务失效问题
2019/10/21 PHP
PHP 出现 http500 错误的解决方法
2021/03/09 PHP
JavaScript脚本性能的优化方法
2007/02/02 Javascript
Extjs TriggerField在弹出窗口显示不出问题的解决方法
2010/01/08 Javascript
Google的跟踪代码 动态加载js代码方法应用
2012/11/12 Javascript
js捕获鼠标滚轮事件代码
2013/12/16 Javascript
JavaScript简单判断复选框是否选中及取出值的方法
2015/08/13 Javascript
jQuery Real Person验证码插件防止表单自动提交
2015/11/06 Javascript
卸载安装Node.js与npm过程详解
2016/08/15 Javascript
JS中动态创建元素的三种方法总结(推荐)
2016/10/20 Javascript
javascript 中模板方法单例的实现方法
2017/10/17 Javascript
Express的HTTP重定向到HTTPS的方法
2018/06/06 Javascript
在vue和element-ui的table中实现分页复选功能
2019/12/04 Javascript
用jQuery实现抽奖程序
2020/04/12 jQuery
[17:13]DOTA2 HEROS教学视频教你分分钟做大人-斯拉克
2014/06/13 DOTA
在Python中操作字典之fromkeys()方法的使用
2015/05/21 Python
pycharm远程调试openstack的图文教程
2017/11/21 Python
Python爬虫工程师面试问题总结
2018/03/22 Python
Python实现的求解最大公约数算法示例
2018/05/03 Python
儿童编程python入门
2018/05/08 Python
keras导入weights方式
2020/06/12 Python
python db类用法说明
2020/07/07 Python
让IE支持CSS3的不完全兼容方案
2014/09/19 HTML / CSS
css3实现二维码扫描特效的示例
2020/10/29 HTML / CSS
医学院学生的自我评价分享
2013/11/19 职场文书
教师岗位聘任书范文
2014/03/29 职场文书
暑假家长评语大全
2014/04/17 职场文书
节约粮食标语
2014/06/18 职场文书
国际商务专业毕业生自我鉴定2014
2014/09/27 职场文书
房屋买卖协议样本
2014/11/16 职场文书
2014年招商工作总结
2014/11/22 职场文书
解决goland 导入项目后import里的包报红问题
2021/05/06 Golang
SQL Server中搜索特定的对象
2022/05/25 SQL Server