Akshare

From sunny-kobe/ai_sentiney · View interactive version

Akshare is an AI agent skill from the GitHub repository sunny-kobe/ai_sentiney. Expert guide for using AkShare to fetch Chinese financial data, covering installation, proxy configuration, resilience patterns, and key interfaces.

How to use this skill

Skill file

AkShare Mastery Skill

AkShare is an open-source financial data interface library for Python, built for human beings! It fetches data from various sources (Eastmoney, Sina, Xueqiu, etc.) primarily via web scraping.

1. Core Philosophy

2. Installation & Mirror

For faster installation in China, use Aliyun mirror:

pip install akshare --upgrade -i https://mirrors.aliyun.com/pypi/simple/

3. Network & Proxy Configuration (Critical)

Since AkShare scrapes websites, it is vulnerable to IP blocking (403 Forbidden / Connection Reset). To use a proxy:

Method A: Environment Variables (Recommended)

Set these before running your script.

export HTTP_PROXY="http://127.0.0.1:7890"
export HTTPS_PROXY="http://127.0.0.1:7890"
python main.py

Method B: Python Context (Targeted)

import os
import akshare as ak

# Apply proxy globally for the process just before calling AkShare
os.environ["HTTP_PROXY"] = "http://127.0.0.1:7890"
os.environ["HTTPS_PROXY"] = "http://127.0.0.1:7890"

df = ak.stock_zh_a_spot_em()

4. Key Interfaces (A-Share)

Real-time Quotes

Historical Data

Market Breadth / Northbound

5. Resilience Pattern (The "Antigravity" Way)

Never call AkShare directly in production without these wrappers:

  1. Timeout Protection: Blocking calls _will_ hang indefinitely if the server holds the connection. Use asyncio.wait_for or func_timeout.
  2. Retries: Use tenacity library.
  3. Validation: Always check if df.empty.
from tenacity import retry, stop_after_attempt
import akshare as ak

@retry(stop=stop_after_attempt(3))
def safe_fetch_spot():
    df = ak.stock_zh_a_spot_em()
    if df is None or df.empty:
        raise ValueError("Empty response")
    return df

6. Troubleshooting

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.