Command Palette

Search for a command to run...

UnylyUnyly
Back to skills

baostock

FreeNo executable scriptsNot checked

BaoStock 免费A股数据平台 - 支持K线、财务数据、行业分类查询,无需注册即可使用。

lxyeternalby lxyeternal★ 68 GitHub stars

About this skill

BaoStock(免费A股数据平台)

BaoStock 是一个免费的开源中国A股证券数据平台。无需注册或API Key,返回 pandas.DataFrame。

安装

pip install baostock --upgrade

验证安装:

python3 -c "import baostock as bs; lg = bs.login(); print(lg.error_msg); bs.logout()"

预期输出:login success!。

基本用法

每个会话必须以 bs.login() 开始,以 bs.logout() 结束:

import baostock as bs
import pandas as pd

# 登录系统
lg = bs.login()

# ... 在此执行数据查询 ...

# 登出系统
bs.logout()

使用 .get_data() 从查询结果中获取DataFrame:

rs = bs.query_all_stock()
df = rs.get_data()

核心API

1. query_all_stock — 获取全部证券列表

获取指定交易日的全部股票/指数代码。

# 获取指定日期所有证券代码
rs = bs.query_all_stock(day="2024-01-02")
df = rs.get_data()
# 返回字段: code(证券代码), tradeStatus(交易状态), code_name(证券名称)
  • day — 日期字符串 YYYY-MM-DD(默认今天)。非交易日返回空DataFrame。

2. query_history_k_data_plus — K线数据

获取历史K线数据(开高低收量 + 指标)。

# 获取工商银行日K线数据
rs = bs.query_history_k_data_plus(
    "sh.601398",
    "date,code,open,high,low,close,volume,amount,pctChg",
    start_date="2024-01-01",
    end_date="2024-06-30",
    frequency="d",       # 频率: d(日线), w(周线), m(月线), 5/15/30/60(分钟线)
    adjustflag="3"       # 复权: 1(后复权), 2(前复权), 3(不复权,默认)
)
df = rs.get_data()

参数说明:

  • code — 股票代码,格式 sh.600000 或 sz.000001
  • fields — 逗号分隔的字段名(见下方)
  • start_date / end_date — YYYY-MM-DD 格式
  • frequency — d(日线), w(周线), m(月线), 5/15/30/60(分钟线)。指数无分钟级数据。
  • adjustflag — 1(后复权), 2(前复权), 3(不复权,默认)

日线可用字段:

date(日期), code(证券代码), open(开盘价), high(最高价), low(最低价), close(收盘价), preclose(昨收价), volume(成交量), amount(成交额), adjustflag(复权标志), turn(换手率), tradestatus(交易状态), pctChg(涨跌幅), peTTM(滚动市盈率), pbMRQ(市净率), psTTM(滚动市销率), pcfNcfTTM(滚动市现率), isST(是否ST)

分钟线可用字段:

date(日期), time(时间), code(证券代码), open(开盘价), high(最高价), low(最低价), close(收盘价), volume(成交量), amount(成交额), adjustflag(复权标志)

3. query_trade_dates — 交易日历

# 获取指定范围的交易日历
rs = bs.query_trade_dates(start_date="2024-01-01", end_date="2024-12-31")
df = rs.get_data()
# 返回字段: calendar_date(日历日期), is_trading_day(是否交易日)

4. query_stock_industry — 行业分类

# 获取全部股票行业分类
rs = bs.query_stock_industry()
df = rs.get_data()
# 返回字段: updateDate(更新日期), code(证券代码), code_name(证券名称), industry(行业), industryClassification(行业分类)

5. query_stock_basic — 股票基本信息

# 获取指定股票基本信息
rs = bs.query_stock_basic(code="sh.601398")
df = rs.get_data()
# 返回字段: code(证券代码), code_name(证券名称), ipoDate(上市日期), outDate(退市日期), type(类型), status(状态)
  • type — 1 股票, 2 指数, 3 其他
  • status — 1 上市, 0 退市

6. query_dividend_data — 分红信息

# 获取指定股票分红数据
rs = bs.query_dividend_data(code="sh.601398", year="2023", yearType="report")
df = rs.get_data()
  • yearType — report(报告期) 或 operate(实施期)

7. 财务数据(季度)

盈利能力

# 获取盈利能力指标(ROE、净利润率、毛利率等)
rs = bs.query_profit_data(code="sh.601398", year=2023, quarter=4)
df = rs.get_data()

营运能力

# 获取营运能力指标(存货周转率、应收账款周转率等)
rs = bs.query_operation_data(code="sh.601398", year=2023, quarter=4)
df = rs.get_data()

成长能力

# 获取成长能力指标(营收同比增长、净利润同比增长等)
rs = bs.query_growth_data(code="sh.601398", year=2023, quarter=4)
df = rs.get_data()

偿债能力

# 获取偿债能力指标(流动比率、速动比率等)
rs = bs.query_balance_data(code="sh.601398", year=2023, quarter=4)
df = rs.get_data()

现金流

# 获取现金流数据
rs = bs.query_cash_flow_data(code="sh.601398", year=2023, quarter=4)
df = rs.get_data()

杜邦分析

# 获取杜邦分析数据(ROE分解:利润率×资产周转率×权益乘数)
rs = bs.query_dupont_data(code="sh.601398", year=2023, quarter=4)
df = rs.get_data()

8. 指数数据

指数成分股

# 获取沪深300成分股
rs = bs.query_hs300_stocks()
df = rs.get_data()

# 获取上证50成分股
rs = bs.query_sz50_stocks()
df = rs.get_data()

# 获取中证500成分股
rs = bs.query_zz500_stocks()
df = rs.get_data()

完整示例: Download Daily K-Line Data and Save as CSV

import baostock as bs
import pandas as pd

# 登录系统
bs.login()

# 获取贵州茅台2024年日K线数据(后复权)
rs = bs.query_history_k_data_plus(
    "sh.600519",
    "date,code,open,high,low,close,volume,amount,pctChg,peTTM",
    start_date="2024-01-01",
    end_date="2024-12-31",
    frequency="d",
    adjustflag="2"  # 后复权
)
df = rs.get_data()

# 保存到CSV文件
df.to_csv("kweichow_moutai_2024.csv", index=False)
print(df.head())

# 登出系统
bs.logout()

股票代码格式

  • 上海证券交易所: sh.600000, sh.601398
  • 深圳证券交易所: sz.000001, sz.300750
  • 北京证券交易所: bj.430047
  • 指数: sh.000001(上证综指), sh.000300(沪深300)

使用技巧

  • 无需注册或API Key — 直接调用 bs.login() 即可开始。
  • 长时间不活动会话可能超时 — 重新调用 bs.login() 即可。
  • 非线程安全 — 并行下载请使用 multiprocessing(多进程),而非threading(多线程)。
  • 数据覆盖范围:A股自1990年至今。
  • 财务数据按季度提供,报告期结束后约有2个月的延迟。
  • Documentation: http://baostock.com/baostock/index.php/Python_API%E6%96%87%E6%A1%A3

进阶示例

批量下载多只股票数据

import baostock as bs
import pandas as pd

bs.login()

# 定义要下载的股票列表
stock_list = ["sh.600519", "sh.601398", "sz.00
Full SKILL.md on GitHub

Install baostock in Claude Code & Claude Desktop

Sign up to install this skill

Create a free account to reveal the install command and save the skill to your library.

  • Reveal the one-line install command
  • Save skills to your synced library
  • Get notified when skills update
Sign up freeI already have an account

Allowed tools

Tools this skill is permitted to call.

No restriction — this skill can use any tool.

Bundled files

_meta.json

FAQ

What does the baostock skill do?

BaoStock 免费A股数据平台 - 支持K线、财务数据、行业分类查询,无需注册即可使用。

How do I install the baostock skill?

Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.

Does the baostock skill run scripts?

No, this skill is instructions only (SKILL.md) with no executable scripts.

Related skills

Compare baostock with