| name | pywencai |
| description | Tonghuashun WenCai natural language data query tool — query A-share, index, fund, HK/US stock, convertible bond, and other market data using Chinese natural language. |
| homepage | https://github.com/zsrl/pywencai |
PyWenCai (Tonghuashun WenCai Data Query)
Query A-share and other market data from Tonghuashun WenCai using Chinese natural language via Python.
⚠️ Cookie Required: You must provide a valid cookie from the WenCai website. See how to obtain a cookie below.
Requirements
- Python 3.7+
- Node.js v16+ (pywencai internally executes JS code)
- pip package manager
Installation
pip install pywencai --upgrade
How to Obtain a Cookie
- Open https://www.iwencai.com/ in your browser and log in.
- Press F12 to open Developer Tools → switch to the Network tab.
- Perform any query on the WenCai page.
- Find a request to
iwencai.com and copy the Cookie value from the request headers.
- Use that string as the
cookie parameter.
Basic Usage
import pywencai
res = pywencai.get(query='今日涨幅前10', cookie='your_cookie_here')
print(res)
API Reference: pywencai.get(**kwargs)
Required Parameters
- query — Chinese natural language query string, e.g.
'今日涨停股票', '市盈率小于20的股票'
- cookie — Cookie string obtained from the WenCai website (required)
Optional Parameters
- sort_key — Sort field name, e.g.
'退市@退市日期'
- sort_order — Sort direction:
'asc' (ascending) or 'desc' (descending)
- page — Page number (default:
1)
- perpage — Results per page (default and max:
100)
- loop — Set to
True to fetch all pages; or set to an integer n to fetch the first n pages
- query_type — Query category (default:
'stock'), possible values:
stock — A-share stocks
zhishu — Indices
fund — Funds
hkstock — Hong Kong stocks
usstock — US stocks
threeboard — New Third Board (NEEQ)
conbond — Convertible bonds
insurance — Insurance
futures — Futures
lccp — Wealth management products
- retry — Number of retries on failure (default:
10)
- sleep — Delay in seconds between paginated requests (default:
0)
- log — Set to
True to print logs to the console
- pro — Set to
True to use the paid version (requires a corresponding cookie)
- no_detail — Set to
True to always return a DataFrame or None (never returns a dict)
- find — List of stock codes to prioritize, e.g.
['600519', '000010']
- request_params — Extra parameters passed to
requests, e.g. {'proxies': proxies}
Return Values
- List-type queries → returns a
pandas.DataFrame
- Detail-type queries → returns a
dict (may contain text and DataFrames)
Usage Examples
Query Stocks with P/E Ratio Below 20
import pywencai
res = pywencai.get(query='市盈率小于20的股票', cookie='xxx')
print(res)
Get Delisted Stocks Sorted by Date
import pywencai
res = pywencai.get(
query='退市股票',
sort_key='退市@退市日期',
sort_order='asc',
cookie='xxx'
)
print(res)
Paginate Through All Data with a Proxy
import pywencai
proxies = {'http': 'http://proxy:8080', 'https': 'http://proxy:8080'}
res = pywencai.get(
query='昨日涨幅',
sort_order='asc',
loop=True,
log=True,
cookie='xxx',
request_params={'proxies': proxies}
)
print(res)
Query Index Data
import pywencai
res = pywencai.get(
query='上证指数近5日涨跌幅',
query_type='zhishu',
cookie='xxx'
)
print(res)
Query Convertible Bond Data
import pywencai
res = pywencai.get(
query='可转债溢价率小于10%',
query_type='conbond',
cookie='xxx'
)
print(res)
Tips
- Use sparingly — High-frequency calls may get you blocked by the WenCai server.
- Always use the latest version:
pip install pywencai --upgrade
- Query strings use Chinese natural language — write queries as you would search on the WenCai website.
- When
loop=True and find is set, loop is ignored and only the first 100 results are returned.
- For paid data, set
pro=True and provide a valid cookie.
Advanced Examples
Query Limit-Up Stock Details
import pywencai
res = pywencai.get(
query='今日涨停的股票',
cookie='xxx'
)
print(res)
Query Consecutive Limit-Up Stocks
import pywencai
res = pywencai.get(
query='连续涨停天数大于2天的股票',
cookie='xxx'
)
print(res)
Query Financial Data
import pywencai
res = pywencai.get(
query='ROE大于15%且营收同比增长率大于20%的股票',
cookie='xxx'
)
print(res)
res = pywencai.get(
query='市盈率小于10且市净率小于1的股票',
cookie='xxx'
)
print(res)
Query Technical Indicator Data
import pywencai
res = pywencai.get(
query='今日MACD金叉的股票',
cookie='xxx'
)
print(res)
res = pywencai.get(
query='KDJ的J值小于0的股票',
cookie='xxx'
)
print(res)
res = pywencai.get(
query='今日成交量是5日均量2倍以上且涨幅大于5%的股票',
cookie='xxx'
)
print(res)
Query Capital Flow Data
import pywencai
res = pywencai.get(
query='今日主力资金净流入前20的股票',
cookie='xxx'
)
print(res)
res = pywencai.get(
query='北向资金持股比例最高的前20只股票',
cookie='xxx'
)
print(res)
Query Fund Data
import pywencai
res = pywencai.get(
query='近一年收益率最高的前20只基金',
query_type='fund',
cookie='xxx'
)
print(res)
Query Hong Kong Stock Data
import pywencai
res = pywencai.get(
query='港股市值最大的前20只股票',
query_type='hkstock',
cookie='xxx'
)
print(res)
Multi-Criteria Stock Screening
import pywencai
res = pywencai.get(
query='市盈率小于20且营收同比增长大于30%且机构持仓比例大于10%的股票',
cookie='xxx'
)
print(res)
res = pywencai.get(
query='今日站上20日均线且市盈率小于30且ROE大于10%的股票',
cookie='xxx'
)
print(res)
Retrieve Historical Data
import pywencai
res = pywencai.get(
query='2024年1月2日涨幅前10的股票',
cookie='xxx'
)
print(res)
res = pywencai.get(
query='2024年上半年涨幅最大的前20只股票',
cookie='xxx'
)
print(res)
Full Example: Automated Stock Screening and Export
import pywencai
import pandas as pd
import time
cookie = 'your_cookie_here'
strategies = {
"低估值高分红": "市盈率小于15且股息率大于3%的股票",
"高成长": "营收同比增长大于30%且净利润同比增长大于30%的股票",
"技术突破": "今日放量突破20日均线且涨幅大于3%的股票",
"机构关注": "近一个月机构调研次数大于3次的股票",
"北向资金": "北向资金今日净买入前20的股票",
}
results = {}
for name, query in strategies.items():
try:
res = pywencai.get(query=query, cookie=cookie, no_detail=True)
if res is not None and not res.empty:
results[name] = res
print(f"Strategy [{name}] selected {len(res)} stocks")
else:
print(f"Strategy [{name}] returned no results")
except Exception as e:
print(f"Strategy [{name}] query failed: {e}")
time.sleep(2)
if results:
with pd.ExcelWriter("选股结果.xlsx") as writer:
name, df results.items():
df.to_excel(writer, sheet_name=name, index=)
()
社区与支持
由 大佬量化 (Boss Quant) 维护 — 量化交易教学与策略研发团队。
微信客服: bossquant1 · Bilibili · 搜索 大佬量化 on 微信公众号 / Bilibili / 抖音