推荐一个提供免费金融数据的 API:挖地兔 https://tushare.pro
这里可以免费提供大量数据,还有现成的 python 模块。
模块安装: >pip install tushare
安装模块后,下载数据导入你的 MySQL 数据库,非常容易。
下面写了个下载数据的实例,试试吧。 https://tushare.pro
实现方法
# -*- coding: utf-8 -*-
import sys
import tushare as ts
import pymysql
if __name__ == '__main__':
# 设置token
ts.set_token('你的token')
# 初始化API接口
pro = ts.pro_api()
# 建立数据库连接
db = pymysql.connect(host='127.0.0.1', user='root', passwd='你的密码', db='stock', charset='utf8')
cursor = db.cursor()
try:
# 获取基础信息数据,包括股票代码、名称、上市日期、退市日期等
df = pro.stock_basic(exchange='', list_status='L',
fields='ts_code, symbol, name, area, industry, fullname, enname, market, exchange, curr_type, list_status, list_date, delist_date, is_hs')
# 获取记录总数
rows = df.shape[0]
except Exception as e:
print(e)
sys.exit()
fail_num = 0
for index in range(rows):
stk_info = list(df.loc[index])
try:
sql_insert = '''INSERT INTO stock_basic (ts_code, symbol, name, area, industry, fullname, enname, market, exchange, curr_type, list_status, list_date, delist_date, is_hs) VALUES ("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")''' % (str(stk_info[0]), str(stk_info[1]), str(stk_info[2]), str(stk_info[3]), str(stk_info[4]), str(stk_info[5]), str(stk_info[6]), str(stk_info[7]), str(stk_info[8]), str(stk_info[9]), str(stk_info[10]), str(stk_info[11]), str(stk_info[12]), str(stk_info[13]))
cursor.execute(sql_insert)
db.commit()
except Exception as e:
print(df.loc[index])
print('ERROR :', e)
fail_num += 1
continue
cursor.close()
db.close()
print(f'\n{rows} records were read, {rows-fail_num} insert successfully, {fail_num} failed.')
print('Done!')
点击注册 tushare ,免费的。
然后就可以获得你自己的 token,然后就有数据玩了。