log
2026-07-09 c4fc32d73b36eb42beed034a375a82ecc992da06
scripts/data_update_v2.py
@@ -19,6 +19,7 @@
import pickle
import json
import argparse
import time
os.makedirs('logs', exist_ok=True)
@@ -278,7 +279,28 @@
    return converted[columns]
def fetch_real_stock_data_from_baostock(n_stocks=500, days=365, stock_list=None):
def reconnect_baostock(bs):
    """BaoStock长时间运行后可能断开连接,重登后可继续请求。"""
    try:
        bs.logout()
    except Exception as e:
        logger.warning(f"⚠️ BaoStock重连前登出失败: {e}")
    login_result = bs.login()
    if getattr(login_result, 'error_code', '0') != '0':
        logger.error(f"❌ BaoStock重新登录失败: {login_result.error_msg}")
        return None
    logger.info("✓ BaoStock已重新登录")
    return login_result
def fetch_real_stock_data_from_baostock(
    n_stocks=500,
    days=365,
    stock_list=None,
    max_retries=2,
    retry_delay=1,
):
    """从BaoStock获取真实股票历史数据,作为东方财富/AKShare不可用时的备用源。"""
    logger.info("="*60)
    logger.info("从BaoStock获取真实A股数据")
@@ -344,6 +366,10 @@
                stock_code = normalize_baostock_code(bs_code)
                stock_name = row['code_name']
            for attempt in range(max_retries + 1):
                if attempt > 0:
                    logger.info(f"    ↻ 重试 {attempt}/{max_retries}: {stock_name}({stock_code})")
            try:
                logger.info(f"  [{position}/{len(stock_list)}] 获取 {stock_name}({stock_code})...")
                result = bs.query_history_k_data_plus(
@@ -357,7 +383,7 @@
                if getattr(result, 'error_code', '0') != '0':
                    logger.warning(f"    ⚠️ 获取失败: {result.error_msg}")
                    continue
                        break
                df = convert_baostock_history(
                    baostock_result_to_dataframe(result),
@@ -370,10 +396,19 @@
                    logger.info(f"    ✓ 获取 {len(df)} 条记录")
                else:
                    logger.warning("    ⚠️ 无数据")
                    break
            except Exception as e:
                logger.warning(f"    ⚠️ 获取失败: {e}")
                continue
                    if attempt >= max_retries:
                        break
                    if retry_delay:
                        time.sleep(retry_delay)
                    login_result = reconnect_baostock(bs)
                    if login_result is None:
                        break
        if all_data:
            combined = pd.concat(all_data, ignore_index=True)