Install pywencai
mainInstall the package using pip. Because the library executes JavaScript code internally, you must have Node.js version v16+ installed on your system before installing the package.
pip install pywencairepository·main·Indexed 21 days ago
https://github.com/zsrl/pywencaiAn open-source Python tool for fetching data from Tonghuashun Wencai (问财) for quantitative research. It provides the pywencai.get() function to query data using parameters such as query, cookie, and query_type, returning results as pandas DataFrames or dictionaries. Requires Node.js v16+ for internal JavaScript execution.
Install the package using pip. Because the library executes JavaScript code internally, you must have Node.js version v16+ installed on your system before installing the package.
pip install pywencaiYou can use the loop parameter to automatically aggregate multiple pages of data into a single DataFrame. You can also pass custom request configurations like proxies via request_params.
import pywencai
# Fetch all pages of data with logging and custom proxies
res = pywencai.get(
query='昨日涨幅',
sort_order='asc',
loop=True,
log=True,
request_params={'proxies': my_proxies}
)The primary way to retrieve data from Tonghuashun Wencai is using the pywencai.get(**kwargs) function.
Important Requirement: Due to login policy changes, you must provide a valid cookie parameter for the request to succeed. You can obtain this by copying the Cookie field value from your browser's request headers when logged into the Wencai website.
Return Values:
pandas.DataFrame.dict which may contain text and DataFrames.import pywencai
# Basic usage with required cookie
res = pywencai.get(query='退市股票', sort_key='退市@退市日期', sort_order='asc', cookie='xxx')
print(res)The get(**kwargs) method accepts the following parameters:
| Parameter | Required | Description |
|---|---|---|
query | Yes | The search query string. (Note: question is deprecated in favor of query). |
cookie | Yes | The Cookie string from your browser headers. |
sort_key | No | The column name to sort by. |
sort_order | No | Sorting direction: 'asc' (ascending) or 'desc' (descending). |
page | No | The page number to query (default: 1). |
perpage | No | Items per page (default: 100, max: 100). |
loop | No | Pagination control. Set to True to fetch all pages, or an integer n to fetch n pages. |
query_type | No | Type of asset. Default is 'stock'. Options: 'stock', 'zhishu', 'fund', 'hkstock', 'usstock', 'threeboard', 'conbond', 'insurance', 'futures', 'lccp', 'foreign_exchange'. |
retry | No | Number of retries on failure (default: 10). |
sleep | No | Seconds to sleep between requests during looping (default: 0). |
log | No | Whether to print logs to the console (default: False). |
pro | No | Set to True for paid version access (requires cookie). |
request_params | No | Dictionary of extra parameters to pass to the underlying requests call (e.g., {'proxies': proxies}). |
no_detail | No | If True, detail queries return None instead of a dict, ensuring consistent return types. |
find | No | An array of identifiers (e.g., ['600519']) to move to the top of the DataFrame. (Note: Disables loop and limits results to 100). |
user_agent | No | Custom User-Agent string. |
The primary entrypoint for the pywencai library is the get() function. It is used to query Wencai data by passing various query parameters as keyword arguments (**kwargs).
from pywencai import get
# Example usage (parameters depend on the specific query type)
# data = get(param1='value', param2='value')