There are many ways to obtain stock data sets through Python, which can be obtained by crawling stock-related websites or through the Tushare library.
Operation goal: Obtain and export a specific stock data set to a CSV file. (Take Kweichow Moutai as an example)
First register and log in at the upper right corner of Tushare
From here, enter directly → After Tushare
registration is completed, you can enter the personal homepage through the upper right corner. You can
get the Token password (that is, the interface command of api) on the personal homepage.
At this time, copy the token password, and we can obtain stock data in Python
import tushare as ts # 导入tushare库
pro = ts.pro_api('我是token命令') # 将复制好的token命令复制进api中
Successfully enter, instantiate df to receive stock data. Let’s take A-share king 600519 "Kweichow Moutai" as an example, and get data for a period of time~
from the beginning to the present (manual dog head)
df = pro.weekly(ts_code='600519.SH', start_data='', end_data='20201119')
# 获取 600519.SH 贵州茅台股票代码
print('贵州茅台df:\n', df)
At this point, we have obtained the weekly data set of Kweichow Moutai stocks from 2001 to the present, and then imported it into the CSV file.
df.to_csv('600519贵州茅台.csv')
Open the file and you're done!
Note: Tushare points are obtained. Generally, after completing the registration, you can achieve 120 points by modifying your personal information.