# -*- coding: UTF-8 -*-
import tushare as ts
from sqlalchemy import create_engine
code_list = []
#数据库链接参数
host = '10.0.0.5'
port = 3306
user = 'root'
password = 'qweqwe'
database = 'stock'
charset = "utf8"
table = "stock_data"
# create table stock_data(id int auto_increment,code int(6) zerofill,date date not null,open decimal(10,2) not null,high decimal(10,2) not null,close decimal(10,2) not null,low decimal(10,2) not null,volume decimal(10,2),turnover decimal(10,2),primary key (id),index(code),index(date),index(open),index(high),index(close),index(low))
def get_data (code):
df = ts.get_hist_data(code)
data = df.iloc[::-1, [0, 1, 2, 3, 4, 13]]
data["code"] = code
return data
def save_data_to_mysql (data,user = 'root',password = 'qweqwe',database = 'stock',charset = "utf8",table = "stock_data"):
engine = create_engine('mysql://' + user + ':' + password + '@' + host + '/' + database + '?charset=' + charset)
data.to_sql(table, engine, if_exists='append')
return
for code in code_list:
try:
data = get_data(code)
save_data_to_mysql(data)
print str(code) + "导入mysql成功"
except:
print str(code) + "获取数据失败"
股票数据存csv。CSV,英文原文为:Comma(逗号)-Separated(分割)Values(数值),有时也称为字符分隔值,CSV文件格式的通用标准并不存在,但是在RFC4180中有基础性的描述。使用的字符编码同样没有被指定,但是bitASCII是最基本的通用编码。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)