关于 Python 连接 MySQL 可参考如下链接
Python操作Mysql - xinhua - 博客园 (cnblogs.com)
由于这里create_engine指定了utf-8字符集,所以对应的bigdata数据库的字符集也需要是utf8,否则在传入中文数据的时候会报错;根据官方API的说明,表则可以不需要自己建
alter database bigdata default character set utf8
import tushare as ts
import pandas as pd
from sqlalchemy import create_engine
import MySQLdb
engine_ts = create_engine('mysql://root:hadoop@hadoop102:3306/bigdata?charset=utf8&use_unicode=1')
ts.set_token('xxxxx')
pro = ts.pro_api()
# df = pro.query('trade_cal', exchange='', start_date='20180901', end_date='20181001', fields='exchange,cal_date,is_open,pretrade_date', is_open='0')
# print(df)
data = pro.query('stock_basic', exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
# num = data.count()
# print(num.loc['ts_code'])
# for i in range(num.loc['ts_code']):
# print(data.loc[i])
# data = pro.query('hsgt_top10', ts_code='600519.SH', start_date='20180701', end_date='20180725')
res = data.to_sql('stock_basic', engine_ts, index=False, if_exists='append', chunksize=5000)
print(res)
# print(data)