使用AKTools部署AKShare财经数据接口库,AKShare的介绍见:基于 Python 的财经数据接口库:AKShare-CSDN博客
AKTools 是一款用于快速搭建 AKShare HTTP API 的工具,通过 AKTools 可以利用一行命令来启动 HTTP 服务,从而让原本专属服务于 Python 用户的开源财经数据接口库 AKShare 的使用 突破编程语言的限制。无论您使用的是 C/C++、Java、Go、Rust、Ruby、PHP、JavaScript、R、Matlab、Stata 等编程语言或软件都可以快速、轻松获取财经数据,助力您更好地展开研究和开发工作。
官网:GitHub - akfamily/aktools: AKTools is an elegant and simple HTTP API library for AKShare, built for AKSharers!
手册:AKTools 官方文档
快速部署使用
安装AKTools
pip install aktools
升级AKTools
pip install aktools -U
启动AKTools服务
安装好后,执行下面命令启动服务:
python -m aktools
如果要让其它机器也能访问,需要绑定ip到0.0.0.0,使用命令:
python -m aktools --host 0.0.0.0
启动后显示:
python -m aktools --host 0.0.0.0
请访问:http://0.0.0.0:8080/version 来获取最新的库版本信息,确保使用最新版本的 AKShare 和 AKTools
当前的 AKTools 版本为:0.0.89,AKShare 版本为:1.15.73
点击打开 HTTP API 主页:http://0.0.0.0:8080/
点击打开接口导览:http://0.0.0.0:8080/docs
INFO: Started server process [2004]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
INFO: 172.25.176.1:60070 - "GET / HTTP/1.1" 200 OK
INFO: 172.25.176.1:60070 - "GET /favicon.ico HTTP/1.1" 200 OK
INFO: 172.25.176.1:61788 - "GET /docs HTTP/1.1" 200 OK
INFO: 172.25.176.1:61788 - "GET /openapi.json HTTP/1.1" 200 OK
INFO: 172.25.176.1:61788 - "GET /api/public/stock_zh_a_hist HTTP/1.1" 200 OK
INFO: 172.25.176.1:61905 - "GET /api/public/600001 HTTP/1.1" 404 Not Found
INFO: 172.25.176.1:61918 - "GET /api/public/600001 HTTP/1.1" 404 Not Found
INFO: 172.25.176.1:61922 - "GET /api/public/stock_zh_a_hist HTTP/1.1" 200 OK
这样其它机器也可以通过使用ip地址来访问这台AKTools的服务了。比如这台机器的ip地址是172.25.183.186,那么就可以通过这个网址来访问:
http://172.25.183.186:8080/
主要,要根据服务器的ip地址修改网址链接,以上只是一个例子。
使用AKTools
无参数调用千股千评
比如原来使用ak.stock_comment_em()
来获取 东方财富网-数据中心-特色数据-千股千评
的数据,现在使用AKTolls,直接访问这个地址即可:
http://172.25.183.186:8080/api/public/stock_comment_em
本机访问
http://127.0.0.1:8080/api/public/stock_comment_em
输出信息如下:
[{"序号": 1,"代码": "000001","名称": "平安银行","最新价": null,"涨跌幅": null,"换手率": 0,"市盈率": 4.21,"主力成本": 11.4905800646,"机构参与度": 0.5175816,"综合得分": 70.24115075,"上升": -335,"目前排名": 425,"关注指数": 85.6,"交易日": "2025-01-15T00:00:00.000"},{"序号": 2,"代码": "000002","名称": "万 科A","最新价": null,"涨跌幅": null,"换手率": 0,"市盈率": -3.42,"主力成本": 6.8563898546,"机构参与度": 0.4192784,"综合得分": 54.83373013,"上升": -410,"目前排名": 4338,"关注指数": 75.6,"交易日": "2025-01-15T00:00:00.000"},{"序号": 3,"代码": "000004","名称": "国华网安","最新价": null,"涨跌幅": null,"换手率": 0,"市盈率": -37.19,"主力成本": 13.5006343582,"机构参与度": 0.173278,"综合得分": 49.38011126,"上升": 74,"目前排名": 5057,"关注指数": 71.2,"交易日": "2025-01-15T00:00:00.000"},
里面的综合得分、机构参与度、关注指数等数据,都可以提供决策帮助。
参数调用
对于带参数的接口,此处分为两种情况。此处以 ak.stock_zh_a_hist()
接口为例,如果直接访问:http://127.0.0.1:8080/api/public/stock_zh_a_hist
则可以获取带默认参数的数据,如果需要为该接口指定参数,比如获取 symbol="600000"
的股票,则只需要访问:http://127.0.0.1:8080/api/public/stock_zh_a_hist?symbol=600000
即可。 注意此处的 ?symbol=600000
中都不带 "
号,如果需要同时指定多个参数,则只需要访问:http://127.0.0.1:8080/api/public/stock_zh_a_hist?symbol=600000&period=weekly
即可。
调用浦发银行的默认日数据
网址:
http://172.25.183.186:8080/api/public/stock_zh_a_hist?symbol=600000
输出信息:
[{"日期": "1999-11-10T00:00:00.000","股票代码": "600000","开盘": 29.5,"收盘": 27.75,"最高": 29.8,"最低": 27,"成交量": 1740850,"成交额": 4859102000,"振幅": 28,"涨跌幅": 177.5,"涨跌额": 17.75,"换手率": 54.4},{"日期": "1999-11-11T00:00:00.000","股票代码": "600000","开盘": 27.58,"收盘": 27.71,"最高": 28.38,"最低": 27.53,"成交量": 294034,"成交额": 821582000,"振幅": 3.06,"涨跌幅": -0.14,"涨跌额": -0.04,"换手率": 9.19},{"日期": "2025-01-15T00:00:00.000","股票代码": "600000","开盘": 10.16,"收盘": 10.21,"最高": 10.39,"最低": 10.13,"成交量": 384608,"成交额": 394080520,"振幅": 2.56,"涨跌幅": 0.49,"涨跌额": 0.05,"换手率": 0.13},{"日期": "2025-01-16T00:00:00.000","股票代码": "600000","开盘": 10.29,"收盘": 10.26,"最高": 10.31,"最低": 10.13,"成交量": 111000,"成交额": 113188998,"振幅": 1.76,"涨跌幅": 0.49,"涨跌额": 0.05,"换手率": 0.04}
]
调用调用浦发银行的数据周线
网址:
http://127.0.0.1:8080/api/public/stock_zh_a_hist?symbol=600000&period=weekly
输出:
{"日期": "2025-01-03T00:00:00.000","股票代码": "600000","开盘": 10.34,"收盘": 10.06,"最高": 10.56,"最低": 10.02,"成交量": 2658623,"成交额": 2736531849,"振幅": 5.21,"涨跌幅": -2.9,"涨跌额": -0.3,"换手率": 0.91},{"日期": "2025-01-10T00:00:00.000","股票代码": "600000","开盘": 10.13,"收盘": 10.13,"最高": 10.37,"最低": 9.81,"成交量": 2394593,"成交额": 2437980129,"振幅": 5.57,"涨跌幅": 0.7,"涨跌额": 0.07,"换手率": 0.82},{"日期": "2025-01-16T00:00:00.000","股票代码": "600000","开盘": 10.06,"收盘": 10.24,"最高": 10.39,"最低": 9.85,"成交量": 1315183,"成交额": 1331291096,"振幅": 5.33,"涨跌幅": 1.09,"涨跌额": 0.11,"换手率": 0.45}
]
可以看到是从1999-11-12开始的周线信息。
Docker部署:AKTools-Docker 版本
下载镜像
# docker pull registry.cn-shanghai.aliyuncs.com/akfamily/aktools:[AKShare 的版本号]
docker pull registry.cn-shanghai.aliyuncs.com/akfamily/aktools:1.15.73
启动镜像
高性能模式
高性能模式为镜像启动后的默认模式,其主要是启动 Gunicorn 作为 WSGI 服务器,提高网络处理的性能,更多资料请参考
常规方式运行:
docker run -p 8080:8080 registry.cn-shanghai.aliyuncs.com/akfamily/aktools:1.15.73
后台运行:
docker run -d -p 8080:8080 registry.cn-shanghai.aliyuncs.com/akfamily/aktools:1.15.73
普通模式
常规方式运行:
docker run -p 8080:8080 registry.cn-shanghai.aliyuncs.com/akfamily/aktools:1.8.95 python -m aktools --host 0.0.0.0 --port 8080
后台运行:
docker run -d -p 8080:8080 registry.cn-shanghai.aliyuncs.com/akfamily/aktools:1.8.95 python -m aktools --host 0.0.0.0 --port 8080
本机访问
http://127.0.0.1:8080/api/public/stock_zh_a_hist
升级镜像
最简单的办法就是启动AKtolls后,查看提示的最新版本号,然后再次docker run的时候直接用最新版本的镜像。
也可以进入镜像,升级AKTools软件:
docker run -it ak_tools:1.15.73 /bin/bash
# 升级 AKShare 到最新版:
pip install akshare -U
退出镜像:exit
找到容器 ID:docker ps -a
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f352c80dd47f registry.cn-shanghai.aliyuncs.com/akfamily/aktools:1.8.95 "python -m aktools -…" 12 minutes ago Up 12 minutes 0.0.0.0:8080->8080/tcp nifty_margulis
提交修改:
docker commit -m "update akshare to latest" f352c80dd47f ak_tools:1.15.74
这样就将AKTools的镜像升级到1.15.74了(当然目前2025.1.16日最新版本是1.15.73,这是举个例子)
最后利用构建好的新镜像启动新容器:docker run -p 8080:8080 ak_tools:
1.15.74