import akshare as ak
import pandas as pd
from multiprocessing.dummy import Pool as ThreadPool
import datetime
import timedef get_hs300_stock_codes():'''获取沪深300股票代码列表:return:'''hs300=ak.index_stock_cons_sina("000300")codes=hs300['code']codes=codes.tolist()return codesdef dwon_data(code, period='daily',from_date='20170301', to_date=datetime.date.today().strftime('%Y-%m-%d'),fq='qfq'):'''下载沪深300成分股历史数据:param symbol: 股票代码,可以在 ak.stock_zh_a_spot_em() 中获取:param period:choice of {'daily', 'weekly', 'monthly'}:param from_date:开始查询的日期:param to_date:结束查询的日期:param fq:复权,默认返回不复权的数据; qfq: 返回前复权后的数据; hfq: 返回后复权后的数据:return:'''df = ak.stock_zh_a_hist(symbol=code, period=period, start_date=from_date, end_date=to_date,adjust=fq)# 获取# df=df.loc[:,['日期','开盘','最高','最低','收盘','成交量']]# df.columns=['datetime','open','high','low','close','volume']print(df.head())# # 构建保存路径与文件名# path_file = 'E:\datas\stock\\hs300\data_suorce\%s.csv' % (code)## # 保存下载数据# df.to_csv(path_file) # 保存文件# print('已导出%s' % (code))if __name__ == '__main__':start_time = time.time()# 获取沪深300代码列表code = get_hs300_stock_codes()# 创建线程池,添加10个线程with ThreadPool(10) as pool:pool.map_async(dwon_data, code) #加载线程pool.close() #pool.join()end_time = time.time()print('程序运行时间:{:.2f}秒'.format(end_time - start_time))