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中的装饰器用法详解
Jan 14 Python
利用Python自动监控网站并发送邮件告警的方法
Aug 24 Python
python取数作为临时极大值(极小值)的方法
Oct 15 Python
由Python编写的MySQL管理工具代码实例
Apr 09 Python
Python3+Appium安装使用教程
Jul 05 Python
python将类似json的数据存储到MySQL中的实例
Jul 12 Python
django最快程序开发流程详解
Jul 19 Python
python实现同一局域网下传输图片
Mar 20 Python
Python虚拟环境venv用法详解
May 25 Python
Python字符串的15个基本操作(小结)
Feb 03 Python
Python实现制作销售数据可视化看板详解
Nov 27 Python
python数字图像处理数据类型及颜色空间转换
Jun 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/10/14 PHP
sae使用smarty模板的方法
2013/12/17 PHP
PHP实用函数分享之去除多余的0
2015/02/06 PHP
使用PHP接受文件并获得其后缀名的方法
2015/08/05 PHP
分享50个提高PHP执行效率的技巧
2015/12/26 PHP
PHP实现根据密码长度显示安全条
2017/07/04 PHP
PHP基于回溯算法解决n皇后问题的方法示例
2017/11/07 PHP
Sample script that displays all of the users in a given SQL Server DB
2007/06/16 Javascript
jquery打开直接跳到网页最下面、最低端实现代码
2013/04/22 Javascript
js禁止回车提交表单的示例代码
2013/12/23 Javascript
jquery实现ajax提交form表单的方法总结
2014/03/03 Javascript
AngularJS内置指令
2015/02/04 Javascript
javascript定时器完整实例
2015/02/10 Javascript
Js 获取当前函数参数对象的实现代码
2016/06/20 Javascript
jsp 网站引入外部css或者js失效问题解决
2016/10/31 Javascript
Javascript前端经典的面试题及答案
2017/03/14 Javascript
利用javascript如何随机生成一定位数的密码
2017/09/22 Javascript
JQueryDOM之样式操作
2019/03/27 jQuery
JavaScript中BOM对象原理与用法分析
2019/07/09 Javascript
vue中实现点击按钮滚动到页面对应位置的方法(使用c3平滑属性实现)
2019/12/29 Javascript
[54:24]Optic vs TNC 2018国际邀请赛小组赛BO2 第二场
2018/08/18 DOTA
Python之eval()函数危险性浅析
2014/07/03 Python
Python实现统计英文单词个数及字符串分割代码
2015/05/28 Python
win10下Python3.6安装、配置以及pip安装包教程
2017/10/01 Python
Python实现感知器模型、两层神经网络
2017/12/19 Python
python使用itchat库实现微信机器人(好友聊天、群聊天)
2018/01/04 Python
Python3.5实现的罗马数字转换成整数功能示例
2019/02/25 Python
利用python numpy+matplotlib绘制股票k线图的方法
2019/06/26 Python
使用Python实现分别输出每个数组
2019/12/06 Python
Python变量作用域LEGB用法解析
2020/02/04 Python
PyCharm无法识别PyQt5的2种解决方法,ModuleNotFoundError: No module named 'pyqt5'
2020/02/17 Python
Jupyter Notebook 文件默认目录的查看以及更改步骤
2020/04/14 Python
Python如何获取文件指定行的内容
2020/05/27 Python
Python新手学习raise用法
2020/06/03 Python
python 提高开发效率的5个小技巧
2020/10/19 Python
八一慰问活动方案
2014/02/07 职场文书