pywencai Documentation

repository·main·Indexed 21 days ago

https://github.com/zsrl/pywencai

An 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.

Tokens
980
Snippets
4
Records
5
Agent score
25%

What's inside pywencai

  1. Install pywencai

    main

    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 pywencai
  2. Advanced usage: Pagination and Proxies

    main

    You 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}
    )
  3. Use pywencai.get() to query Wencai data

    main

    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:

    • For list-based queries, it returns a pandas.DataFrame.
    • For detail-based queries, it returns a 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)
  4. Reference: pywencai.get() parameters

    main

    The get(**kwargs) method accepts the following parameters:

    ParameterRequiredDescription
    queryYesThe search query string. (Note: question is deprecated in favor of query).
    cookieYesThe Cookie string from your browser headers.
    sort_keyNoThe column name to sort by.
    sort_orderNoSorting direction: 'asc' (ascending) or 'desc' (descending).
    pageNoThe page number to query (default: 1).
    perpageNoItems per page (default: 100, max: 100).
    loopNoPagination control. Set to True to fetch all pages, or an integer n to fetch n pages.
    query_typeNoType of asset. Default is 'stock'. Options: 'stock', 'zhishu', 'fund', 'hkstock', 'usstock', 'threeboard', 'conbond', 'insurance', 'futures', 'lccp', 'foreign_exchange'.
    retryNoNumber of retries on failure (default: 10).
    sleepNoSeconds to sleep between requests during looping (default: 0).
    logNoWhether to print logs to the console (default: False).
    proNoSet to True for paid version access (requires cookie).
    request_paramsNoDictionary of extra parameters to pass to the underlying requests call (e.g., {'proxies': proxies}).
    no_detailNoIf True, detail queries return None instead of a dict, ensuring consistent return types.
    findNoAn array of identifiers (e.g., ['600519']) to move to the top of the DataFrame. (Note: Disables loop and limits results to 100).
    user_agentNoCustom User-Agent string.
  5. Query Wencai data with get()

    main

    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')