量化策略多因子选股之抓取数据并保存到本机MYSQL数据库

数据来自于tushare

使用了sqlalchemy 包的引擎来保存数据与SQL


# coding=utf-8
import pandas as pd
import tushare as ts
import sys
from sqlalchemy import create_engine

reload(sys)
sys.setdefaultencoding("utf-8")


zz500=ts.get_zz500s()
hs300=ts.get_hs300s()

zz500_code= zz500['code']
hs300_code= hs300['code']

get_industry= ts.get_industry_classified()

print get_industry
# 得到sina行业分类
get_industry_hs300= get_industry[get_industry['code'].isin(hs300_code) ]
get_industry_zz500= get_industry[get_industry['code'].isin(zz500_code) ]


#得到2017年第四季度的主报数据
df1=get_report=ts.get_report_data(2017,4)


#得到2017年第四季度的盈利能力数据
df2=ts.get_profit_data(2017,4)


#得到2017年第四季度的成长能力数据
df3=ts.get_growth_data(2017,4)


engine = create_engine('mysql://root:********@127.0.0.1/stock_report?charset=utf8')

df1.to_sql('get_report',engine)
df2.to_sql('get_report2',engine)
df3.to_sql('get_report3',engine)

frames = [get_industry_hs300, get_industry_zz500]
result = pd.concat(frames)
result.to_sql('hangye',engine)

数据预览:

量化策略多因子选股之抓取数据并保存到本机MYSQL数据库