import tushare as ts import MySQLdb from config import CONFIG_DB from config import CONFIG_TS db = MySQLdb.connect(CONFIG_DB['host'], CONFIG_DB['user'], CONFIG_DB['password'], CONFIG_DB['database'], charset='utf8') pro = ts.pro_api(CONFIG_TS['secret']) df = pro.stock_basic(**{ "ts_code": "", "name": "", "exchange": "", "market": "", "is_hs": "", "list_status": "", "limit": "", "offset": "" }, fields=[ "ts_code", "symbol", "name", "area", "industry", "market", "list_date", "fullname", "enname", "cnspell", "exchange", "curr_type", "list_status", "delist_date", "is_hs" ]) cursor = db.cursor() for row in df.itertuples(): sql = "insert into t_ods_hs_base_list(ts_code, symbol, name, area, industry, fullname, enname, \ cnspell, market, exchange, curr_type, list_status, list_date, is_hs) \ values('%s', '%s', '%s', '%s', '%s', '%s', \"%s\", '%s', '%s', '%s', '%s', '%s', '%s', '%s') \ on duplicate key update symbol= '%s', name= '%s', area='%s', industry='%s', fullname='%s', \ enname=\"%s\", cnspell='%s', market='%s', exchange='%s', curr_type='%s', list_status='%s', \ list_date='%s', is_hs='%s' " % \ (getattr(row, 'ts_code'), getattr(row, 'symbol'), getattr(row, 'name'), getattr(row, 'area') , getattr(row, 'industry'), getattr(row, 'fullname'), getattr(row, 'enname'), getattr(row, 'cnspell') , getattr(row, 'market'), getattr(row, 'exchange'), getattr(row, 'curr_type'), getattr(row, 'list_status') , getattr(row, 'list_date'), getattr(row, 'is_hs'), getattr(row, 'symbol'), getattr(row, 'name'), getattr(row, 'area') , getattr(row, 'industry'), getattr(row, 'fullname'), getattr(row, 'enname'), getattr(row, 'cnspell') , getattr(row, 'market'), getattr(row, 'exchange'), getattr(row, 'curr_type'), getattr(row, 'list_status') , getattr(row, 'list_date'), getattr(row, 'is_hs')) try: cursor.execute(sql) db.commit() except Exception as ex: print (ex) db.rollback() break db.close()