import pandas as pd #import tushare as ts from sqlalchemy import create_engine import baostock as bs #### 登陆系统 #### lg = bs.login() # 显示登陆返回信息 print('login respond error_code:'+lg.error_code) print('login respond error_msg:'+lg.error_msg) #### 获取沪深A股历史K线数据 #### # 详细指标参数,参见“历史行情指标参数”章节;“分钟线”参数与“日线”参数不同。 # 分钟线指标:date,time,code,open,high,low,close,volume,amount,adjustflag rs = bs.query_history_k_data_plus("sh.600000", "date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST", start_date='2017-07-01', end_date='2017-12-31', frequency="d", adjustflag="3") print('query_history_k_data_plus respond error_code:'+rs.error_code) print('query_history_k_data_plus respond error_msg:'+rs.error_msg) #### 打印结果集 #### data_list = [] while (rs.error_code == '0') & rs.next(): # 获取一条记录,将记录合并在一起 data_list.append(rs.get_row_data()) result = pd.DataFrame(data_list, columns=rs.fields) #### 结果集输出到csv文件 #### result.to_csv("D:\\history_A_stock_k_data.csv", index=False) print(result) #engine=create_engine('mysql+pymysql://root:1234@127.0.0.1/mysql?charset=utf8mb4') engine=create_engine('mysql+pymysql://root:1234@127.0.0.1/mysql?charset=utf8') #result.to_sql(name='sh600000',con=engine,schema='mysql',if_exists='append',index_label='date') data = pd.read_csv('D:\\history_A_stock_k_data.csv') data.to_sql('mysql', con=engine, if_exists='replace') print('success to sql!') engine.dispose()
读取CSV数据并写入MySQL
于 2024-03-06 13:33:54 首次发布
本文介绍了如何使用Python的Tushare和Baostock库获取沪深A股的历史K线数据,包括日期、开盘价等,然后将数据存储到MySQL数据库中。
摘要由CSDN通过智能技术生成