因为自己最近在学习做期货交易,想要下载期货的行情数据来做分析。有一些交易软件是可以导出数据的,但是导出的过程还是需要很多的手工操作,自己在想能不能通过Python程序来实现呢。
新浪期货数据接口介绍
上面的例子是请求玉米连续(代码为C0)的所有日线图的行情数据。
利用Python来获取数据
下面是详细的代码:
import requests
import sys
future_code = sys.argv[1]
url_str = ('http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesDailyKLine?symbol=' +
future_code)
r = requests.get(url_str)
r_json = r.json()
r_lists = list(r_json)
print('date,open,high,low,close,vol,code')
for r_list in r_lists:
for v in r_list:
print(v + ',',end='')
print(future_code)
代码说明:使用python的第三方模块requests可以很方便的对api发送请求,将返回的json格式数据转换为列表格式,并且通过for循环来打印出来。
脚本的使用方法,将上面的代码保存为get_sina_api_data.py。在命令行中通过下面命令来调用程序。
#参数中输入期货代码C0
$ python3 get_