Remove files
This commit is contained in:
+46
-13
@@ -21,26 +21,59 @@ class ChinaStockData:
|
||||
def get_stock_list(self):
|
||||
"""获取A股股票列表"""
|
||||
try:
|
||||
# 获取沪深A股实时行情
|
||||
stock_info = ak.stock_zh_a_spot_em()
|
||||
import requests
|
||||
# 设置较短的超时时间,避免长时间等待
|
||||
import akshare as ak
|
||||
|
||||
print("正在获取A股股票列表...")
|
||||
|
||||
# 尝试获取沪深A股实时行情,设置超时时间
|
||||
try:
|
||||
# 临时设置requests的默认超时
|
||||
original_timeout = getattr(requests, 'timeout', None)
|
||||
requests.timeout = 10 # 10秒超时
|
||||
|
||||
stock_info = ak.stock_zh_a_spot_em()
|
||||
|
||||
# 恢复原始超时设置
|
||||
if original_timeout:
|
||||
requests.timeout = original_timeout
|
||||
else:
|
||||
delattr(requests, 'timeout')
|
||||
|
||||
except Exception as network_error:
|
||||
print(f"网络请求失败: {network_error}")
|
||||
# 网络失败时返回空列表,让调用方使用备用方案
|
||||
return []
|
||||
|
||||
if stock_info is None or len(stock_info) == 0:
|
||||
print("获取到的股票数据为空")
|
||||
return []
|
||||
|
||||
# 增加到前2000只股票,提供更多选择
|
||||
stock_list = []
|
||||
for index, row in stock_info.head(2000).iterrows():
|
||||
# 过滤掉ST股票和停牌股票
|
||||
stock_name = str(row['名称'])
|
||||
if 'ST' not in stock_name and '*' not in stock_name:
|
||||
stock_list.append({
|
||||
'symbol': row['代码'],
|
||||
'name': row['名称'],
|
||||
'price': float(row['最新价']) if pd.notna(row['最新价']) else 0.0,
|
||||
'change_pct': float(row['涨跌幅']) if pd.notna(row['涨跌幅']) else 0.0,
|
||||
'volume': float(row['成交量']) if pd.notna(row['成交量']) else 0.0,
|
||||
'amount': float(row['成交额']) if pd.notna(row['成交额']) else 0.0
|
||||
})
|
||||
try:
|
||||
# 过滤掉ST股票和停牌股票
|
||||
stock_name = str(row['名称'])
|
||||
if 'ST' not in stock_name and '*' not in stock_name:
|
||||
stock_list.append({
|
||||
'symbol': row['代码'],
|
||||
'name': row['名称'],
|
||||
'price': float(row['最新价']) if pd.notna(row['最新价']) else 0.0,
|
||||
'change_pct': float(row['涨跌幅']) if pd.notna(row['涨跌幅']) else 0.0,
|
||||
'volume': float(row['成交量']) if pd.notna(row['成交量']) else 0.0,
|
||||
'amount': float(row['成交额']) if pd.notna(row['成交额']) else 0.0
|
||||
})
|
||||
except Exception as row_error:
|
||||
print(f"处理股票数据行时出错: {row_error}")
|
||||
continue
|
||||
|
||||
# 按成交金额排序,优先显示活跃股票
|
||||
stock_list.sort(key=lambda x: x['amount'], reverse=True)
|
||||
print(f"成功获取 {len(stock_list)} 只股票")
|
||||
return stock_list
|
||||
|
||||
except Exception as e:
|
||||
print(f"获取股票列表失败: {e}")
|
||||
return []
|
||||
|
||||
Reference in New Issue
Block a user