Registry / data / baostock

baostock

JSON →
library0.9.2pypypi✓ verified 73d ago

Baostock is a Python library designed to provide free historical data for the China stock market, sourced from Baostock.com. It allows users to retrieve various types of data, including daily K-line data, stock basic information, and transaction details, making it a valuable tool for quantitative analysis and research. The current version is 0.9.1, and releases typically occur to fix bugs or adapt to changes in the Baostock data service.

pip install baostock
INSTALL
IMPORT
SIG · BAOSTOCK
B
baostock
datapythonv0.9.2
Install
7.7s avg
Import
966ms
Disk
165MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.9.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.995s · 165.2MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 7.7s · import 0.937s · 158MB
165MB installed
● package 165MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

baostock
import baostock as bs
from baostock import login
The library is designed for a single import alias 'bs' for all core functions.

This quickstart demonstrates how to log into the Baostock service, query historical daily K-line data for a specific stock ('sh.600000'), display the first few rows of the resulting DataFrame, and finally log out. Ensure `BAOSTOCK_USERNAME` and `BAOSTOCK_PASSWORD` environment variables are set or replace the placeholders with your actual Baostock credentials.

import baostock as bs import pandas as pd import os # Baostock login credentials can be set as environment variables (recommended for production) # For local testing, replace with actual username/password or remove os.environ.get if hardcoding (not recommended) USERNAME = os.environ.get('BAOSTOCK_USERNAME', 'your_username') PASSWORD = os.environ.get('BAOSTOCK_PASSWORD', 'your_password') if not USERNAME or not PASSWORD or USERNAME == 'your_username': print("Warning: Please set BAOSTOCK_USERNAME and BAOSTOCK_PASSWORD environment variables or replace placeholders.") print("Attempting login with placeholder credentials, which will likely fail.") # Using placeholder for runnable example if env vars not set, but it will likely fail a real login USERNAME = "PLACEHOLDER_USER" PASSWORD = "PLACEHOLDER_PASS" # Login to Baostock service print("Attempting to log in to Baostock...") lg = bs.login(USERNAME, PASSWORD) if lg.error_code == '0': print(f"Login success! User ID: {lg.uid}") else: print(f"Login failed! Code: {lg.error_code}, Msg: {lg.error_msg}") # For a real application, you might want to exit here: import sys; sys.exit(1) # Only proceed if login was successful or if you want to test query failure if lg.error_code == '0': # Query historical K-data for a stock (e.g., 'sh.600000' for Pudong Development Bank) # Fields: date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,peTTM,pbMRQ,psTTM,pcfNCM,isST print("Querying historical K-data for sh.600000...") rs = bs.query_history_k_data_plus("sh.600000", "date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,peTTM,pbMRQ,psTTM,pcfNCM,isST", start_date='2023-01-01', end_date='2023-01-31', frequency="d", adjustflag="2") # frequency="d" means daily, adjustflag="2" means front-adjust data_list = [] if rs.error_code == '0': while rs.next(): data_list.append(rs.get_row_data()) result = pd.DataFrame(data_list, columns=rs.fields) print("\nSample K-data for sh.600000:") print(result.head()) # Logout bs.logout() print("\nLogout successful.") else: print("Skipping data query due to login failure.")
Debug
Known issues
gotchaLogin failures are common due to incorrect credentials, network issues, or Baostock service instability. Always check the `error_code` and `error_msg` returned by `bs.login()`.
fix
Verify username and password, check network connectivity, and refer to `lg.error_msg` for specific issues. Consider retries for transient network errors.
affects: All versions
gotchaData retrieved for certain tickers or time ranges might be incomplete or empty, especially for delisted stocks or periods far in the past/future. The `query_history_k_data_plus` method does not always raise an error for empty results.
fix
Validate the resulting DataFrame's size and content. Cross-reference with other data sources if data completeness is critical. Ensure the stock code and date range are valid.
affects: All versions
gotchaThe Baostock service might occasionally be slow or unresponsive, leading to connection timeouts or delays. The library's underlying `requests` calls might hang or fail.
fix
Implement error handling with retries and exponential backoff. You might need to adjust network timeout settings if `requests` library allows it or handle `requests.exceptions.Timeout`.
affects: All versions
gotchaRepeated, rapid requests can lead to IP-based rate limiting or temporary bans from the Baostock server, causing requests to fail with generic errors or connection issues.
fix
Introduce delays (`time.sleep()`) between successive queries, especially when fetching data for multiple stocks or long historical periods. Optimize data fetching to minimize the number of API calls.
affects: All versions
Errors
Common errors & fixes
Login failed! Code: 10001, Msg: User name or password error!
Incorrect username or password provided during `bs.login()`.
fix
Double-check your Baostock account credentials. Ensure no leading/trailing spaces or typos. If using environment variables, verify they are correctly set.
_bs_client is not login!
An attempt was made to query data (e.g., `bs.query_history_k_data_plus()`) before successfully logging into the Baostock service with `bs.login()`.
fix
Ensure `bs.login()` is called and returns a successful response (`lg.error_code == '0'`) before attempting any data retrieval operations.
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
Network issues, firewall blocking, or the Baostock server prematurely closing the connection. This can also happen if the server is under heavy load or temporarily down.
fix
Check your internet connection and firewall settings. Try again after some time. If persistent, this might indicate a problem with the Baostock service itself or a transient network issue between you and the server.
DataFrame is empty after query, e.g., print(result.empty) -> True
No data was returned for the specified stock code, date range, or query parameters. This often happens for delisted stocks, incorrect stock codes, or periods without available data.
fix
Verify the stock code is correct (e.g., 'sh.600000' or 'sz.000001'). Adjust the `start_date` and `end_date` to ensure data should exist. Check if the stock was active during the queried period. Ensure `rs.error_code` is '0' before processing `rs.next()`.
Upgrade
Version history
0.9.2latest on PyPI · released Jun 6, 2026
Audit
Dependencies
requestsrequiredUsed for making HTTP requests to the Baostock API.
pandasrequiredRequired for data manipulation and returning query results as DataFrames.
numpyrequiredFundamental package for numerical operations, often used by pandas.
beautifulsoup4requiredUsed for parsing HTML content, likely for session management or specific data extraction.
Agent activity
320 hits · last 30 days
node
296
Perplexity
1
Resources
baostock — pip install baostock · libregistry