Skip to main content

mairui — 麦蕊智数 Python SDK

完整接口字段说明见 API.md(含沪深数据中心 / 基金 / 量化因子)。方法对照见 API_METHOD_MAP.md。

面向 https://api.mairuiapi.com,覆盖官网 API 文档(如 https://mairuiapi.com/hsdata)中对外公开的全部接口。

源码仓库:https://github.com/MaiRuiApi/mairui

安装

pip install -U mairui==1.2.1

1.2.1:与五语统一热修发版(本 SDK 由 requests 自动解压;建议同步升级)。

PyPI:https://pypi.org/project/mairui/

30 秒上手

from mairui import Client

with Client(licence="YOUR-LICENCE-UUID") as api:
    # 股票列表
    stocks = api.stock_list()

    # 日线历史(可选 st/et/lt)
    bars = api.stock_history("000001.SZ", "d", "n", st="20240101", et="20241231")

    # 实时
    tick = api.stock_real_time("000001")

    # 财务
    bal = api.stock_balance("000001.SZ")

    # 批量并发(复用连接池)
    batch = api.map(
        api.stock_history,
        ["000001.SZ", "600519.SH", "000002.SZ"],
        period="d",
        dividend="n",
        lt=5,
        max_workers=8,
    )

只需绑定一次 licence,之后所有方法自动拼到 URL 末段。

设计要点(效率与工程)

措施 说明
requests.Session + 连接池 默认 pool_connections=32 / pool_maxsize=64,避免每次握手
Gzip 由 requests 默认协商并自动解压(勿自行关闭解压后硬编码 Accept-Encoding)
自动重试 对 429/502/503/504 指数退避重试(仅 GET)
超时 默认连接 5s / 读 60s,可构造时覆盖
api.map(...) 线程池批量拉多标的,适合回测灌数
路径编码 板块名等中文/特殊字符自动 quote
异常分层 MairuiAuthError / MairuiTimeoutError / MairuiHTTPError

接口 ↔ 方法对照(摘要)

完整对照见 API_METHOD_MAP.md。常用:

文档分类 示例方法
股票列表 stock_list / sectors_list / sector_detail
行情 / 指标 stock_history / stock_latest / stock_macd / stock_vip_history
实时 stock_real_time / stock_ssjy / stock_ssjy_more / stock_ssjy_all
财务 stock_balance / stock_income / stock_financial(table, code)
F10 company_profile / company_dividends / …
股池 limit_up_pool(date) / limit_down_pool / …
指数 index_history / index_macd / index_list
北交所 bj_history / bj_balance / …
基金 / 科创 fund_list / etf_list / star_real_time

查询参数与文档一致:

  • st / et:开始 / 结束时间(如 20240101 或 20240101153000)
  • lt:最新条数(从尾部截取)

不传则返回服务端完整可用数据(由证书权限与数据存量决定)。

构造参数

Client(
    licence="...",
    base_url="https://api.mairuiapi.com",  # 一般无需改
    timeout=(5, 60),
    max_retries=3,
    pool_connections=32,
    pool_maxsize=64,
)

异常处理

from mairui import Client, MairuiAuthError, MairuiHTTPError, MairuiTimeoutError

try:
    with Client(licence="...") as api:
        print(api.stock_history("000001.SZ", "d", "n", lt=3))
except MairuiAuthError as e:
    print("证书问题", e)
except MairuiTimeoutError as e:
    print("超时", e)
except MairuiHTTPError as e:
    print("HTTP", e.status_code, e.payload)

目录

mairui/
  __init__.py
  client.py      # 全部接口方法
  _http.py       # Session / 重试 / 编码
  exceptions.py
examples/
  quickstart.py
pyproject.toml
README.md
API_METHOD_MAP.md
LICENSE

说明

  • 接口清单与官网文档页一致,例如沪深 A 股:https://mairuiapi.com/hsdata(同站还有指数、基金等文档入口)。
  • SDK 默认请求主站 https://api.mairuiapi.com;证书可在官网获取。
  • 本包为同步 SDK;若需 asyncio,可在外层用 asyncio.to_thread 包一层,或后续再发 async 版。

Metadata

Release files for mairui 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mairui 1.2.1
File Size Uploaded Provenance
mairui-1.2.1.tar.gz 16.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for mairui 1.2.1
File Interpreter ABI Platform
mairui-1.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 32.0 kB

Release files / mairui-1.2.1.tar.gz

Download URL mairui-1.2.1.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
7fc78808f44734ad421e9bdbe11892336edf5cb100ccdf62993bcf0ae5067a77
BLAKE2b-256 checksum
How to use checksums
4cce76f85cb230b2b76aef7c5f2d4572a8a5f86d0850c1fe1c5dfbe015419474
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.9

Release files / mairui-1.2.1-py3-none-any.whl

Download URL mairui-1.2.1-py3-none-any.whl
Size 15.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ed22707785e78c3bbff7dfc679057cd4e5e3a3b01b9ed223c35d6206fff56a4d
BLAKE2b-256 checksum
How to use checksums
14862a5887d50a56d8a971a920445e3bb54cf26d07a9f52a34fccbe17c71f2e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.9

Security for mairui 1.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page