线程池下载沪深300股票数据 数据来源:akshare 代码如下,可直接用
import akshare as ak
import pandas as pd
from multiprocessing.dummy import Pool as ThreadPool
import datetime
import time
def get_hs300_stock_codes():
'''
获取沪深300股票代码列表
:return:
'''
hs300=ak.index_stock_cons_sina("000300")
codes=hs300['code']
codes=codes.tolist()
return codes
def 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)
print(df.head())
if __name__ == '__main__':
start_time = time.time()
code = get_hs300_stock_codes()
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))
|