DataBull Python SDK

一条 pip 命令,把 A 股、港股与美股的行情、财务报表,连同 ETF、指数、申万板块与财经快讯接进你的 Python 项目。覆盖 42 个 REST 端点与 38 个 MCP 工具。

当前版本 v0.1.5

1 安装

从 PyPI 安装。核心依赖只有 requests, 需要 *_history 系列方法返回 DataFrame 时再加 pandas。

终端
pip install databull pip install "databull[pandas]" # 需要 DataFrame 时

国内直连 PyPI 慢,或者在内网里,可以直接装官网托管的最新 wheel:

终端
pip install https://www.databull.cn/sdk/files/databull-0.1.5-py3-none-any.whl

不想装包也可以只下一个文件(含全部方法,仅依赖 requests):

终端
curl -fsSLO https://www.databull.cn/sdk/files/databull_client.py

API Key 在控制台免费创建, 免费档 500 次/天。

2 快速开始

Python
from databull import DataBull, DataBullError client = DataBull("dbt_xxxxxxxx") # 也可只设环境变量 DATABULL_API_KEY # 个股日线(返回带日期索引的 DataFrame) df = client.get_stock_history("600519", "2026-01-01", "2026-09-01") # 指数搜索与成分股权重 hits = client.search_index("沪深300", limit=5) cons = client.get_index_constituents("000300", page_size=10)["data"] # 申万一级行业涨跌(sw1 / sw2 / sw3) sectors = client.get_sector_data("sw1")

命令行也可以直接探接口,不用写代码:

终端
python -m databull cn/index/search --params '{"keyword": "沪深300"}'

3 能力清单

下列方法均已在 DataBull 生产 API 验证可用。

能力 方法 路径
指数历史日线 get_index_history GET /cn/index/history
指数清单(分页 + 过滤) get_index_list GET /cn/index/list
指数搜索(相关度排序) search_index GET /cn/index/search
指数成分股与权重 get_index_constituents GET /cn/index/constituents
A股/ETF/指数标的清单 get_stock_list GET /cn/stocks
个股基本信息 get_stock_info GET /cn/stock/info
公司简介 get_company_profile GET /cn/stock/profile
个股历史行情 get_stock_history GET /cn/stock/history
个股财务报表 get_stock_financial_data GET /cn/stock/financial_data
美股信息 / 历史 get_us_stock_info / history GET /us/stock/*
港股信息 / 历史 get_hk_stock_info / history GET /hk/stock/*
ETF 清单 / 基本信息 get_etf_list / get_etf_info GET /cn/etfs/
ETF 申赎清单(PCF) get_etf_pcf GET /cn/etfs/etf_pcf
ETF 成分股 get_etf_composition GET /cn/etfs/etf_composition
ETF 历史净值 / 行情 get_etf_history GET /cn/etf/history
财经快讯 / 来源列表 get_news_flash / _sources GET /v2/news/flash
市场新闻列表 / 详情 get_market_news / _detail GET /cn/market/news
恐惧与贪婪指数 get_fear_greed GET /cn/market/fear_greed
申万板块涨跌幅 get_sector_data GET /cn/market/sector_data/{sw1,sw2,sw3}
申万行业清单 get_sector_catalog GET /cn/market/sector_catalog/{sw1,sw2,sw3}
个股实时 Tick get_realtime GET /cn/stock/tick
交易日历 get_trading_calendar GET /cn/tick/trading_calendar
MCP 工具调用 call_mcp_tool POST /mcp/call

完整字段与示例见接口文档; MCP 已暴露 get_stock_list / get_stock_detail / get_stock_history / get_index_history / get_financial_data 五个工具。

4 三个约定

出错不静默

所有失败都抛 DataBullError, 带 HTTP 状态码与原始响应体。401 = Key 无效,403 = 超出套餐额度,429 = 调用频率超限。 不要用「返回 None」判断失败——那会把请求失败误当成「这个标的没数据」。

空结果带原因

没有数据时接口返回空数组/空对象而不是报错,原因挂在 client.last_empty_reason: index_not_found / no_data_in_range / calendar_not_loaded / no_match / sector_data_not_collected / tick_not_uploaded / catalog_not_synced / constituents_not_synced。

代码一律用裸码

600519 而不是 600519.SH,市场由接口决定。传后缀时个股会 400, 指数不报错但静默返回空数组。SDK 已自动剥离后缀。 另外 market 有两种含义:基址前缀是 cn/us/hk,指数清单的过滤条件是 SSE/SZSE/CSI/SW (SDK 里叫 index_market,传错会提前拦下)。

5 从旧客户端迁移

旧方法名保留为别名,存量代码可以不改: get_history → get_stock_history、get_market_sector → get_sector_data、 get_company → get_company_profile、get_last_tick → get_realtime、 get_market_fear_greed → get_fear_greed。但出错语义已变: 旧版是「打印日志后返回 None」,现在是抛 DataBullError,依赖 None 分支的代码需要改成 except DataBullError。