---
name: akshare-mastery-4fa61edcd51d
description: Expert guide for using AkShare to fetch Chinese financial data, covering installation, proxy configuration, resilience patterns, and key interfaces.
---

# 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

- **Data Source Aggregator**: AkShare isn't a data provider itself; it's a collected interface for scraping other providers.
- **Dependency**: If the upstream source (e.g., Eastmoney) changes its API or blocks IPs, AkShare breaks.
- **No Native Proxy Config**: It uses standard Python `requests`. Proxies must be set via standard environment variables.

## 2. Installation & Mirror

For faster installation in China, use Aliyun mirror:

```bash
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.

```bash
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)

```python
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

- **Spot Data (All Stocks)**: `ak.stock_zh_a_spot_em()`
  - Source: Eastmoney
  - Columns: 代码, 名称, 最新价, 涨跌幅, ...
  - Note: High failure rate if IP is dirty.

### Historical Data

- **Daily K-line**: `ak.stock_zh_a_hist(symbol="000001", period="daily", start_date="20230101", adjust="qfq")`
  - Source: Eastmoney
  - Symbols: "000001" (no prefix usually).

### Market Breadth / Northbound

- **Northbound Funds**: `ak.stock_hsgt_fund_flow_summary_em()`

## 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`.

```python
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

- **Connection Reset / Timeout**: 99% chance your IP is blocked by Eastmoney. Switch IP or wait 2-24 hours.
- **AttributeError**: AkShare updates frequently. Run `pip install akshare --upgrade`.
- **"Zero Data"**: Check if today is a trading day.


---
Source attribution: sunny-kobe
Original project: https://github.com/sunny-kobe/ai_sentiney/blob/3f30de8e0b6645c9df0a2e7764b6ca523c4689b7/.agent/skills/akshare/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2024 Sentinel Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: sunny-kobe
Original project: https://github.com/sunny-kobe/ai_sentiney/blob/3f30de8e0b6645c9df0a2e7764b6ca523c4689b7/.agent/skills/akshare/SKILL.md
Attribution basis: Source repository record

Fields of work: Data & Analytics, Development & Engineering, Finance & Accounting
Roles: Data Analyst, Financial Analyst, ML / AI Engineer
Specialties: API
Tools: Akshare
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 8d67b325b56ed35104bb320dff88752468ebd390d692d8bec5ddcd399ef369cc
