auto-sync: 2026-04-17 20:23:28

This commit is contained in:
cfdaily
2026-04-17 20:23:28 +08:00
parent 5cb2b13b0c
commit a5fce664d3
@@ -44,21 +44,51 @@ class AStockListFetcher:
# 使用akshare获取A股股票列表
stocks_df = self.ak.stock_zh_a_spot()
logger.info(f"成功获取A股股票列表,共 {len(stocks_df)} 只股票")
logger.info(f"返回列名: {list(stocks_df.columns)}")
# 整理列名,选择需要的字段
# akshare返回列名:代码,名称,最新价,涨跌幅,涨跌额,买入,卖出,成交量,成交额,开盘,最高,最低,昨收
# 不同版本akshare返回列名可能有差异,做兼容处理
result_df = pd.DataFrame()
result_df['code'] = stocks_df['代码']
result_df['name'] = stocks_df['名称']
result_df['current_price'] = stocks_df['最新价']
result_df['change_percent'] = stocks_df['涨跌幅']
result_df['change_amount'] = stocks_df['涨跌额']
result_df['volume'] = stocks_df['成交量']
result_df['amount'] = stocks_df['成交额']
result_df['open'] = stocks_df['开盘']
result_df['high'] = stocks_df['最高']
result_df['low'] = stocks_df['最低']
result_df['pre_close'] = stocks_df['昨收']
# 检查列名,兼容不同版本
column_mapping = {
'代码': 'code',
'名称': 'name',
'最新价': 'current_price',
'涨跌幅': 'change_percent',
'涨跌额': 'change_amount',
'成交量': 'volume',
'成交额': 'amount',
'开盘': 'open',
'最高': 'high',
'最低': 'low',
'昨收': 'pre_close'
}
for source_col, target_col in column_mapping.items():
if source_col in stocks_df.columns:
result_df[target_col] = stocks_df[source_col]
# 如果列名是中文但不同命名方式,尝试其他可能
if result_df.empty:
logger.warning("默认列名匹配失败,尝试其他列名匹配...")
# 可能的其他列名
alt_mapping = {
'symbol': 'code',
'name': 'name',
'price': 'current_price',
'changepercent': 'change_percent',
'change': 'change_amount',
'volume': 'volume',
'amount': 'amount',
'open': 'open',
'high': 'high',
'low': 'low',
'settlement': 'pre_close'
}
for source_col, target_col in alt_mapping.items():
if source_col in stocks_df.columns:
result_df[target_col] = stocks_df[source_col]
else:
# AKShare不可用,生成模拟数据