def main(): import logging # define the log file, file mode and logging level logging.basicConfig(filename='dayStockData.log', filemode="w", level=logging.DEBUG) logging.debug('开始爬取数据。Getting Stock data from 163.') # 爬取程序,每天存储一个文件夹。 from ConstConfig import FILEPATH filepath = FILEPATH.DayDataDIRwin.value #filepath = 'd:\\data\\' # 定义数据文件保存路径 import datetime delta = datetime.timedelta(days=0) today = datetime.date.today() - delta todaystr = str(today.strftime('%Y%m%d')) filepath = filepath + todaystr + "\\" print(filepath) from tools import mkdir mkdir.mkdirA(filepath) toCVS(filepath, isalldate=False) # 存储数据库 toMySQL(filepath)
def get_all_stock2(): import tools.platformPrint as pp import tools.mkdir as mkdir if pp.UsePlatform() == "Linux": mkdir.mkdirA('/volume/stock_data') filepath = '/volume/stock_data/get_stock_basics.csv' elif pp.UsePlatform() == "Windows": mkdir.mkdirA('E:\\stock_data\\') filepath = 'E:\\stock_data\\get_stock_basics.csv' try: pro = getPro() stock_info = pro.stock_basic( exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date') stock_info.to_csv(filepath, encoding="gbk", index=True) #获得股票列表,一读一写是为了防止网络问题 except Exception as e: import traceback print('网络获取list错误,从磁盘获取吧。traceback.print_exc():', traceback.print_exc()) logger.info(e) stock_info = pd.read_csv(filepath, dtype=str, encoding="gbk") normal_stocks = stock_info.loc[:, ['symbol', 'name']] #print(normal_stocks) #tms2 = pd.read_csv(filepath, dtype=str, encoding="gbk") #del tms2 #tms3 = pd.read_csv(filepath, dtype=str, encoding="gbk") #del tms3 #del pd #print(tms) #print(list(tms.code)) #delset = tms.loc[:,['code']] try: #normal_stocks.drop(list(tms.code)) print('############################') except Exception as e: import traceback print('traceback.print_exc():', traceback.print_exc()) logger.info(e) finally: ks = normal_stocks #del pd #print(ks) del stock_info, normal_stocks #, tms import gc gc.collect() return ks
def main(): # 爬取程序,每天存储一个文件夹。 filepath = 'e:\\tick_data\\' # 定义数据文件保存路径 import datetime today = datetime.date.today() todaystr = str(today.strftime('%Y%m%d')) filepath = filepath + todaystr + "\\" print(filepath) from tools import mkdir mkdir.mkdirA(filepath) toCVS(filepath)
def get_all_stock2(): import tools.platformPrint as pp import tools.mkdir as mkdir if pp.UsePlatform() == "Linux": mkdir.mkdirA('/volume/stock_data') filepath = '/volume/stock_data/get_stock_basics.csv' elif pp.UsePlatform() == "Windows": mkdir.mkdirA('E:\\stock_data\\') filepath = 'E:\\stock_data\\get_stock_basics.csv' stock_info = ts.get_stock_basics() stock_info.to_csv(filepath, encoding="gbk", index=True) #获得股票列表,一读一写是为了防止网络问题 normal_stocks = stock_info.loc[:, ['name']] #print(normal_stocks) tms = pd.read_csv(filepath, dtype=str, encoding="gbk") #tms2 = pd.read_csv(filepath, dtype=str, encoding="gbk") #del tms2 #tms3 = pd.read_csv(filepath, dtype=str, encoding="gbk") #del tms3 #del pd #print(tms) #print(list(tms.code)) #delset = tms.loc[:,['code']] try: normal_stocks.drop(list(tms.code)) except Exception as e: import traceback print('traceback.print_exc():', traceback.print_exc()) logger.info(e) finally: ks = normal_stocks #del pd #print(ks) del stock_info, normal_stocks, tms import gc gc.collect() return ks
import tushare as ts import os from tools import mkdir mkdir.mkdirA("d:/HS300Days") list = ts.get_hs300s() #print(list) filename = 'd:/day/bigfile.csv' for index, row in list.iterrows(): #print(index) code = row['code'] print(code) filename = 'd:/HS300Days/%s.csv' %code df = ts.get_hist_data(code) if os.path.exists(filename): df.to_csv(filename, mode='a', header=None) else: df.to_csv(filename)
import tushare as ts from tools import mkdir mkdir.mkdirA("d:/day") df = ts.get_hist_data('300022') #直接保存 df.to_csv('d:/day/300022.csv') #print(ts.get_k_data('600000')) print(ts.get_k_data('300022', ktype='5')) #选择保存 #df.to_csv('d:/day/000875.csv',columns=['open','high','low','close'])