注册账号https://tushare.pro/
获取账号的token
将token复制到代码ts.set_token的*******************
获取所有股票的基本信息
import tushare as ts
ts.set_token('***********************')
pro = ts.pro_api()
#查询当前所有正常上市交易的股票列表
data = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
print(data)
data.to_excel('data_0.xlsx') # 将DataFrame导入到Excel当中
获取单股或者多股的每日股票信息
import tushare as ts
ts.set_token('e9339680699eb0aea7d0194887533972cbb72d41659933187ddd0b18')
pro = ts.pro_api()
#查询当前所有正常上市交易的股票列表
df = pro.daily(ts_code='000001.SZ', start_date='20100701', end_date='20210112')
#多个股票
#df = pro.daily(ts_code='000001.SZ,600000.SH', start_date='20180701', end_date='20180718')
print(df)
结果如下
ts_code trade_date open ... pct_chg vol amount
0 000001.SZ 20210112 20.39 ... 3.0422 1288165.50 2.657687e+06
1 000001.SZ 20210111 20.00 ... 2.6700 1790457.14 3.652547e+06
2 000001.SZ 20210108 19.90 ... -0.2513 1195473.22 2.348316e+06
3 000001.SZ 20210107 19.52 ... 1.7382 1584185.30 3.111275e+06
4 000001.SZ 20210106 18.08 ... 7.6500 1934945.12 3.648522e+06
... ... ... ... ... ... ... ...
2491 000001.SZ 20100908 17.91 ... -2.1600 490035.78 8.656646e+05
2492 000001.SZ 20100907 18.34 ... -0.8800 392852.63 7.112746e+05
2493 000001.SZ 20100906 17.86 ... 2.4800 609069.62 1.106419e+06
2494 000001.SZ 20100903 18.17 ... -2.3600 624129.22 1.110874e+06
2495 000001.SZ 20100902 19.00 ... 3.8800 1634542.10 2.984709e+06
[2496 rows x 11 columns]