#-*- coding: utf-8 -*- import pandas as pd import pymysql from sqlalchemy import create_engine dbconn = pymysql.connect( host="127.0.0.1", database="test", user="root", password="111111", port=3306, charset='utf8') connt = create_engine('mysql+mysqldb://root:111111@localhost:3306/test?charset=utf8') import tushare as ts ts.set_token('79acf3c40ec0f32a2510645fe2f84461900550e3edc36feb5fa289b8') pro = ts.pro_api() data = pd.DataFrame(pro.stock_basic(exchange_id='', is_hs='', fields='ts_code,name,list_date,list_status')) codelist = data['ts_code'] for code in codelist: df = pd.DataFrame(pro.daily(ts_code = code, start_date='20180701', end_date='20180702')) if type(df) == pd.DataFrame: df['code'] = code if df.shape[0] > 0: try: pd.io.sql.to_sql(df, "tushare_daily", con=connt, if_exists='append') # 只能说使用createegien的方式 except Exception as e: print 'Error', e else: pass else: print u'怎么没数据' else: print 'No data @', code
从新的tushare获取数据
最新推荐文章于 2024-06-28 16:42:31 发布