fix: 改为每次请求一支股票,间隔9秒
- 移除批量请求逻辑 - 每次只请求一支股票 - 请求完成后休息9秒再请求下一支 - 显示预估耗时
This commit is contained in:
@@ -24,11 +24,8 @@ LOGS_DIR.mkdir(exist_ok=True)
|
||||
START_DATE = '20100101'
|
||||
END_DATE = datetime.now().strftime('%Y%m%d')
|
||||
|
||||
# 每批次获取的股票数量(tushare限制)
|
||||
BATCH_SIZE = 50
|
||||
|
||||
# 请求间隔(秒)- 避免频繁请求
|
||||
REQUEST_INTERVAL = 0.3
|
||||
# 请求间隔(秒)- tushare积分限制
|
||||
REQUEST_INTERVAL = 9
|
||||
|
||||
|
||||
def setup_tushare(token=None):
|
||||
@@ -94,29 +91,32 @@ def get_stock_codes_with_suffix(df):
|
||||
|
||||
|
||||
def fetch_daily_data(pro, codes, start_date, end_date):
|
||||
"""批量获取日线数据"""
|
||||
"""逐个获取日线数据(每次一支股票)"""
|
||||
all_data = []
|
||||
total = len(codes)
|
||||
|
||||
for i in range(0, total, BATCH_SIZE):
|
||||
batch_codes = codes[i:i + BATCH_SIZE]
|
||||
ts_codes = ','.join(batch_codes)
|
||||
|
||||
print(f"\n共 {total} 只股票需要获取")
|
||||
print(f"预计耗时: {total * REQUEST_INTERVAL / 60:.1f} 分钟")
|
||||
print("-" * 50)
|
||||
|
||||
for i, ts_code in enumerate(codes):
|
||||
try:
|
||||
print(f"获取第 {i+1}-{min(i+BATCH_SIZE, total)} 只股票数据...")
|
||||
df = pro.daily(ts_code=ts_codes, start_date=start_date, end_date=end_date)
|
||||
print(f"[{i+1}/{total}] 获取 {ts_code}...", end=' ')
|
||||
|
||||
df = pro.daily(ts_code=ts_code, start_date=start_date, end_date=end_date)
|
||||
|
||||
if df is not None and len(df) > 0:
|
||||
all_data.append(df)
|
||||
print(f" 成功获取 {len(df)} 条记录")
|
||||
print(f"成功,{len(df)} 条记录")
|
||||
else:
|
||||
print(f" 无数据")
|
||||
print("无数据")
|
||||
|
||||
except Exception as e:
|
||||
print(f" 错误: {e}")
|
||||
print(f"错误: {e}")
|
||||
|
||||
# 避免请求过快
|
||||
time.sleep(REQUEST_INTERVAL)
|
||||
# 每次请求后休息9秒
|
||||
if i < total - 1: # 最后一个不需要等待
|
||||
time.sleep(REQUEST_INTERVAL)
|
||||
|
||||
return all_data
|
||||
|
||||
|
||||
Reference in New Issue
Block a user