ZhuLinsen/daily_stock_analysis · error · DataFetchError
Baostock 查询失败: {rs.error_msg}
Error message
Baostock 查询失败: {rs.error_msg} What it means
A DataFetchError raised after a successful baostock login when bs.query_history_k_data_plus(...) returns a result set with error_code != '0'. Baostock reports per-query failures through the result object (not exceptions), so the fetcher checks error_code explicitly and surfaces error_msg; common codes include network receive failures ('网络接收错误') and invalid parameters.
Source
Thrown at data_provider/baostock_fetcher.py:236
bs_code = self._convert_stock_code(stock_code)
logger.debug(f"调用 Baostock query_history_k_data_plus({bs_code}, {start_date}, {end_date})")
with self._baostock_session() as bs:
try:
# 查询日线数据
# adjustflag: 1-后复权,2-前复权,3-不复权
rs = bs.query_history_k_data_plus(
code=bs_code,
fields="date,open,high,low,close,volume,amount,pctChg",
start_date=start_date,
end_date=end_date,
frequency="d", # 日线
adjustflag="2" # 前复权
)
if rs.error_code != '0':
raise DataFetchError(f"Baostock 查询失败: {rs.error_msg}")
# 转换为 DataFrame
data_list = []
while rs.next():
data_list.append(rs.get_row_data())
if not data_list:
raise DataFetchError(f"Baostock 未查询到 {stock_code} 的数据")
df = pd.DataFrame(data_list, columns=rs.fields)
return df
except Exception as e:
if isinstance(e, DataFetchError):
raise
raise DataFetchError(f"Baostock 获取数据失败: {e}") from e
View on GitHub (pinned to 5159bd72e8)
Solutions
- Log rs.error_msg — baostock's message names the exact problem (network receive error vs invalid code).
- For '网络接收错误' style messages, retry once after a short sleep; these are transient.
- Validate the converted bs_code format ('sh.600519'/'sz.000001') and date order before querying.
- Fall back to Akshare for A-shares if the error persists.
Example fix
# before
rs = bs.query_history_k_data_plus(code=bs_code, ...)
assert rs.error_code == '0' # crashes opaquely
# after
rs = bs.query_history_k_data_plus(code=bs_code, ...)
if rs.error_code != '0':
logger.warning('baostock query error %s: %s', rs.error_code, rs.error_msg)
if '网络' in rs.error_msg:
time.sleep(5) # then retry once
else:
raise DataFetchError(f'Baostock 查询失败: {rs.error_msg}') Defensive patterns
Strategy: retry
Validate before calling
import re
assert re.fullmatch(r'(sh|sz)\.\d{6}', bs_code), f'bad baostock code: {bs_code}'
assert start_date <= end_date and re.fullmatch(r'\d{4}-\d{2}-\d{2}', start_date) Try / catch
try:
df = baostock_fetcher.fetch(code, start, end)
except DataFetchError as e:
if '查询失败' in str(e) and '网络' in str(e):
time.sleep(5)
df = baostock_fetcher.fetch(code, start, end) # transient receive error
else:
raise Prevention
- Validate 'sh.NNNNNN'/'sz.NNNNNN' code format and date ordering before querying.
- Retry only network-class error_msg values; parameter errors are permanent.
- Keep an Akshare fallback for A-shares when baostock queries keep failing.
When it happens
Trigger: Calling query_history_k_data_plus with a malformed bs_code (e.g. 'sh.60051' typo), a date range where start > end, baostock socket receive errors mid-stream, or the service rejecting 'pctChg' field availability for some codes. The login succeeded, so credentials are fine — this is query-level.
Common situations: Bad stock code conversion feeding an invalid sh./sz. prefix; baostock server dropping the connection mid-query under load; querying suspended/ST codes with missing fields; date strings in the wrong 'YYYY-MM-DD' format.
Related errors
- Baostock 登录失败: {login_result.error_msg}
- Baostock 未查询到 {stock_code} 的数据
- Baostock 获取数据失败: {e}
- read_failed
- internal_error
AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15).
Data as JSON: /api/errors/8de0d00a4b6deb7e.
Report an issue: GitHub.