安装
pip3 install tushare
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px 'Andale Mono'; color: #2fff12; background-color: #000000; background-color: rgba(0, 0, 0, 0.9)} span.s1 {font-variant-ligatures: no-common-ligatures}
hlz@hlzs-iMac-Pro ~ % pip3 install tushare
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: tushare in /usr/local/lib/python3.7/site-packages (1.2.62)
Requirement already satisfied: websocket-client>=0.57.0 in /usr/local/lib/python3.7/site-packages (from tushare) (0.57.0)
Requirement already satisfied: requests>=2.0.0 in /usr/local/lib/python3.7/site-packages (from tushare) (2.24.0)
Requirement already satisfied: bs4>=0.0.1 in /usr/local/lib/python3.7/site-packages (from tushare) (0.0.1)
Requirement already satisfied: lxml>=3.8.0 in /usr/local/lib/python3.7/site-packages (from tushare) (4.6.2)
Requirement already satisfied: simplejson>=3.16.0 in /usr/local/lib/python3.7/site-packages (from tushare) (3.17.2)
Requirement already satisfied: six in /usr/local/lib/python3.7/site-packages (from websocket-client>=0.57.0->tushare) (1.13.0)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/site-packages (from requests>=2.0.0->tushare) (1.25.10)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/site-packages (from requests>=2.0.0->tushare) (2020.6.20)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/site-packages (from requests>=2.0.0->tushare) (3.0.4)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/site-packages (from requests>=2.0.0->tushare) (2.10)
Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.7/site-packages (from bs4>=0.0.1->tushare) (4.9.3)
Requirement already satisfied: soupsieve>1.2; python_version >= "3.0" in /usr/local/lib/python3.7/site-packages (from beautifulsoup4->bs4>=0.0.1->tushare) (2.0.1)
可以看到其要求有多个依赖库。
获取tushare 的数据接口token
注册
官方网站地址:https://tushare.pro/
也可以选择我的推荐链接来注册,这样可以给我攒点积分
https://tushare.pro/register?reg=422953
获得token
注册完毕后,验证,登陆,点击右上角头像,个人主页,接口token,如下图所示即为token
使用tushare
# 引入tushare 包
import tushare as ts
# 设定token
ts.set_token("xxxx")
# 调用api
pro = ts.pro_api()
# 获取数据
data = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
# 显示所有正常上市交易的股票列表前15
data.head(15)
结果
调用每日行情
dailydata = pro.daily(ts_code='000001.SZ', start_date='20180701', end_date='20180718')
dailydata.head(10)
结果显示如下: