Micro-sheep / Micro-sheep/efinance
search_quote 的两层问题及修复建议
- Dominant language
- Python
- Stars
- 4.1k
- Forks
- 755
- PR merge metrics
- No merged PRs in 30d
Description
## 问题概述
`efinance/utils/__init__.py` 的 `search_quote` 函数存在两层问题,导致股票代码搜索完全失效。
---
### 1. `searchapi.eastmoney.com` 已被东方财富废弃
2026 年 7 月起,该接口被东方财富彻底改写:
- 请求参数 `type=14`(股票搜索)无论传什么值,均返回同一个股吧用户主页数据(`result.passportWeb`)
- 响应格式从纯 JSON 变为 JSONP(`jQuery...({...})` 包裹)
该接口**已不再提供股票搜索功能**。`.json()` 解析 JSONP 直接失败(尤其环境安装 `simplejson` 时,抛出的是
`requests.exceptions.JSONDecodeError`,与 `except json.JSONDecodeError` 不重合,该 catch 形同虚设);即使剥掉 JSONP
外层,拿到的也是无用数据。
**修复**:剥离 JSONP 外层后,校验关键字段,不存在则返回 `None`:
```python
import re
text = resp.text
match = re.search(r'\((\{.*\})\)', text, re.DOTALL)
if match:
text = match.group(1)
try:
data = json.loads(text)
except Exception:
return None
if "QuotationCodeTable" not in data:
return None
items = data["QuotationCodeTable"]["Data"]
---
2. 对 searchapi 过度强依赖
search_quote 唯一作用是股票名/代码 → 东方财富内部 quote_id(如 1.600519)。
但 A 股的「名字→6位代码」是公开且稳定的,可从交易所直接获取。quote_id 格式也完全可预测——就是 市场编号.证券代码:
┌──────────────────┬──────┬───────────────┐
│ 市场 │ 编号 │ 代码特征 │
├──────────────────┼──────┼───────────────┤
│ 沪A/科创板/沪ETF │ 1 │ 60/68/5 开头 │
├──────────────────┼──────┼───────────────┤
│ 深A/创业板/深ETF │ 0 │ 00/30/15 开头 │
├──────────────────┼──────┼───────────────┤
│ 港股 │ 116 │ 5 位 │
└──────────────────┴──────┴───────────────┘
修复:增加本地映射,优先本地解析,searchapi 只作兜底:
def _code_to_quote_id(code):
if code[0] in ('6', '5'):
return f'1.{code}'
return f'0.{code}'
def search_quote(keyword, ...):
# 已有:查缓存 ...
# 新增:纯数字代码直接构造 quote_id
if keyword.isdigit() and len(keyword) in (5, 6):
quote_id = _code_to_quote_id(keyword)
return Quote(code=keyword, quote_id=quote_id, ...)
# 原有:调 searchapi ...
长远来看,从交易所拉取 A 股全量列表(a kshare 的 stock_info_a_code_name()
即做此事)预置为名字→代码映射,则中文名称也无需搜索,searchapi 仅作为缓存未命中时的最后兜底。
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in efinance/utils/__init__.py at search_quote and read the existing cache and searchapi path. Verify handling of JSONP and missing QuotationCodeTable data, then check that numeric five- or six-digit codes produce the expected quote_id while searchapi remains a fallback; done means stock-code searches no longer fail silently or depend on the obsolete response.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend-api-design, search
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 58/100