Use python to obtain stock data in real time for analysis
Article directory
python crawler learns to crawl stock data in real time, python obtains stock data analysis in real time
Preparation
-
Go to tushare official website to obtain the interface token, official website: tushare official website
-
Install tushare library
Enter the following command on the command line to install the Tushare library:
pip install tushare
Sample script
Basic information of listed companies
Description: Obtain basic information of listed companies. 4,500 items can be extracted at a time. It can be extracted in batches according to the exchange.
# 导入tushare
import tushare as ts
# 初始化pro接口
pro = ts.pro_api('****************************')
df = pro.stock_company(exchange='SZSE', fields='ts_code,chairman,manager,secretary,reg_capital,setup_date,province')
print(df)
"""输出:
ts_code chairman manager secretary reg_capital setup_date province
0 002992.SZ 李军 张春 张国宏 18595.1135 20060810 广东
1 300292.SZ 万卫方 张建国 李勇 134176.4974 19990622 江苏
2 002553.SZ 史建伟 姜宗成 史维 34800.0000 19980508 江苏
3 300721.SZ 刘准 刘准 蔡国庆 16484.6739 19960620 江苏
4 300455.SZ 李永 王涛 曹昶辉 71776.7936 20070904 北京
... ... ... ... ... ... ... ...
2918 300341.SZ 杨文良 杨泽声 李臻 51339.3236 20021104 福建
2919 300840.SZ 张蕴蓝 张蕴蓝 吕显洲 24000.0000 20071228 山东
2920 003026.SZ 徐一俊 徐一俊 李志萍 10063.3516 20100125 浙江
2921 300575.SZ 吴耀军 吴耀军 陆洋 46475.6400 20030807 江苏
2922 002978.SZ 罗阳勇 李顺泽 周立 40100.0000 19940405 四川
[2923 rows x 7 columns]
"""
A-share daily quotes
Data description: Storage is carried out between 15:00 and 16:00 every day on the trading day. This interface is the market before the rights are restored, and no data is provided during the suspension period.
df = pro.daily(ts_code='000001.SZ', start_date='20180701', end_date='20180718')
#多个股票
df = pro.daily(ts_code='000001.SZ,600000.SH', start_date='20180701', end_date='20180718')
# 或者
df = pro.query('daily', ts_code='000001.SZ', start_date='20180701', end_date='20180718')
#也可以通过日期取历史某一天的全部历史
df = pro.daily(trade_date='20180810')
"""输出
ts_code trade_date open high low close pre_close change pct_chg vol amount
0 000001.SZ 20180718 8.75 8.85 8.69 8.70 8.72 -0.02 -0.23 525152.77 460697.377
1 000001.SZ 20180717 8.74 8.75 8.66 8.72 8.73 -0.01 -0.11 375356.33 326396.994
2 000001.SZ 20180716 8.85 8.90 8.69 8.73 8.88 -0.15 -1.69 689845.58 603427.713
3 000001.SZ 20180713 8.92 8.94 8.82 8.88 8.88 0.00 0.00 603378.21 535401.175
4 000001.SZ 20180712 8.60 8.97 8.58 8.88 8.64 0.24 2.78 1140492.31 1008658.828
5 000001.SZ 20180711 8.76 8.83 8.68 8.78 8.98 -0.20 -2.23 851296.70 744765.824
6 000001.SZ 20180710 9.02 9.02 8.89 8.98 9.03 -0.05 -0.55 896862.02 803038.965
7 000001.SZ 20180709 8.69 9.03 8.68 9.03 8.66 0.37 4.27 1409954.60 1255007.609
8 000001.SZ 20180706 8.61 8.78 8.45 8.66 8.60 0.06 0.70 988282.69 852071.526
9 000001.SZ 20180705 8.62 8.73 8.55 8.60 8.61 -0.01 -0.12 835768.77 722169.579
"""
illustrate
For more data interfaces, please visit: tushare official website