不适合64位系统,但是的确是有64位的版本,建议安装mysql-connector-python-2.1.5-py2.7-winx64.exe
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
from sqlalchemy import create_engine
import tushare as ts
import pandas as pd
#建立与本地数据库的连接
engine=create_engine('mysql://user:[email protected]/stock?charset=utf8')
#获取3120只股票代码
df=ts.get_stock_basics()
Code=df.index
#日期格式YYYYMMDD转为YYYY-MM-DD
def formatDate(Date, formatType='YYYYMMDD'):
formatType = formatType.replace('YYYY', Date[0:4])
formatType = formatType.replace('MM', Date[4:6])
formatType = formatType.replace('DD', Date[-2:])
return formatType
#查询每只股票的上市日期
for code in Code:
#code='300455'
date=df.ix[code]['timeToMarket'] #上市日期YYYYMMDD
date=formatDate(str(date),'YYYY-MM-DD') #改一下格式
dh=ts.get_k_data(code,start=date,end='2017-12-31') #不复权
dg=ts.get_k_data(code,start=date,end='2017-12-31',autype='hfq') #后复权
del dg['code']
del dg['date']
del dg['volume']
dg.rename(columns={'open':'fopen', 'close':'fclose', 'high':'fhigh', 'low':'flow'}, inplace = True)
mergeColumn=pd.concat([dh,dg],axis=1)
mergeColumn.to_sql('history',engine,if_exists='append')
数据库连接需要填入自己的user和password。