感谢:https://github.com/shidenggui/easyquotation
使用该库搭建在线获取股票信息的接口,并将其使用flask库转化成http服务可供调用
import easyquotation import flask from flask import request from flask import jsonify server = flask.Flask(__name__) quotation = easyquotation.use('sina') @server.route('/getStock', methods=['get']) def run(): # 获取通过url请求传参的数据 stockId = request.values.get('stockId') res = quotation.real(stockId) return jsonify(res) if __name__ == '__main__': server.run(debug=True, port=5194, host='0.0.0.0')
docker run python:3.9
拉取前最好将docker的源配置成阿里云的,网上有教程,镜像大小大约900M
sudo docker create -it -p 5194:5194 python:3.9
一定要将端口先映射出来,否则以后创建成功后再映射端口就很麻烦
docker exec -it [容器id] bash
先将pip升级并且换源,临时的方法:
pip install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade pip
踩坑:一定要是https,否则警告
pip install -i https://mirrors.aliyun.com/pypi/simple/ easyquotation
pip install -i https://mirrors.aliyun.com/pypi/simple/ flask
环境搭建完毕!!!
服务器地址:
/opt/stock/run.py
docker cp /opt/stock/run.py [容器id]:/opt/
nohup python3 run.py &
现在容器内部:
curl 127.0.0.1:5194/getStock?stockId=300025
再到服务器测试:
curl 127.0.0.1:5194/getStock?stockId=300025