python 调用AKShare股市数据分析:一站式获取A股各类板块材料

AKShare股市数据分析:一站式获取A股各类板块数据

简介

AKShare是一个开源的Python金融数据接口库,本教程将展示如何使用AKShare获取A股市场的各类板块数据,包括行业板块、概念板块、风格板块等。

环境准备

安装AKShare

# 安装akshare
pip install akshare --upgrade
# 国内用户建议使用镜像源安装
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple akshare --upgrade

导入必要的库

import akshare as ak
import pandas as pd

板块数据获取方法

1. 获取行业板块数据

1.1 申万行业分类
def get_sw_industry_stocks():
"""获取申万行业分类数据"""
# 获取申万一级行业列表
sw_index = ak.sw_index_spot()
print("申万一级行业列表:")
print(sw_index[['指数代码', '指数名称']])
# 获取特定行业成分股
industry_code = "801010" # 农林牧渔
stocks = ak.sw_index_cons(index_code=industry_code)
print(f"\n行业 {industry_code
} 成分股:")
print(stocks[['成分股代码', '成分股名称']])
# 调用示例
get_sw_industry_stocks()
1.2 证监会行业分类
def get_csrc_industry():
"""获取证监会行业分类数据"""
# 获取证监会行业分类
industry_data = ak.stock_industry_category_cninfo()
# 按行业分组统计
industry_summary = industry_data.groupby('行业分类名称')['股票代码'].apply(list)
return industry_summary
# 调用示例
csrc_industries = get_csrc_industry()
print("证监会行业分类:")
print(csrc_industries)

2. 概念板块数据

2.1 同花顺概念板块
def get_ths_concept():
"""获取同花顺概念板块数据"""
# 获取概念板块列表
concepts = ak.stock_board_concept_name_ths()
print("同花顺概念板块列表:")
print(concepts)
# 获取特定概念板块成分股
concept_stocks = ak.stock_board_concept_cons_ths(symbol="人工智能")
print("\n人工智能概念股:")
print(concept_stocks[['代码', '名称']])
# 调用示例
get_ths_concept()
2.2 东方财富概念板块
def get_em_concept():
"""获取东方财富概念板块数据"""
# 获取概念板块行情
concept_quotes = ak.stock_board_concept_name_em()
print("东方财富概念板块行情:")
print(concept_quotes)
# 获取特定概念板块成分股
concept_stocks = ak.stock_board_concept_cons_em(symbol="车联网")
print("\n车联网概念股:")
print(concept_stocks)
# 调用示例
get_em_concept()

3. 风格板块数据

def get_style_index():
"""获取风格指数数据"""
# 获取中证风格指数行情
style_index = ak.index_style_spot_em()
print("中证风格指数:")
print(style_index)
# 调用示例
get_style_index()

4. 地域板块数据

def get_region_stocks():
"""获取地域板块数据"""
# 获取地域板块行情
region_quotes = ak.stock_board_industry_name_em()
region_data = region_quotes[region_quotes['板块名称'].str.contains('地区')]
print("地域板块行情:")
print(region_data)
# 获取特定地域成分股
region_stocks = ak.stock_board_industry_cons_em(symbol="浙江板块")
print("\n浙江板块成分股:")
print(region_stocks)
# 调用示例
get_region_stocks()

数据分析示例

1. 板块涨跌幅分析

def analyze_sector_performance():
"""分析各板块涨跌幅"""
# 获取概念板块行情
concept_quotes = ak.stock_board_concept_name_em()
# 按涨跌幅排序
performance = concept_quotes.sort_values('涨跌幅', ascending=False)
print("今日表现最好的概念板块:")
print(performance.head())
print("\n今日表现最差的概念板块:")
print(performance.tail())
# 调用示例
analyze_sector_performance()

2. 板块成分股分析

def analyze_sector_stocks(sector_name):
"""分析特定板块的成分股表现"""
# 获取板块成分股
stocks = ak.stock_board_concept_cons_em(symbol=sector_name)
# 获取成分股行情
stock_quotes = ak.stock_zh_a_spot_em()
# 合并数据
sector_analysis = pd.merge(stocks, stock_quotes, left_on='代码', right_on='代码')
# 按涨跌幅排序
return sector_analysis.sort_values('涨跌幅', ascending=False)
# 调用示例
result = analyze_sector_stocks("新能源")
print("新能源板块成分股表现:")
print(result[['代码', '名称', '最新价', '涨跌幅']])

⚠️ 注意事项

  1. 数据时效性

    • AKShare的数据大多为实时更新
    • 建议在交易时段获取数据
    • 部分数据可能有延迟
  2. 接口限制

    • 注意访问频率限制
    • 建议增加适当的延时
    import time
    time.sleep(1) # 每次请求间隔1秒
  3. 数据存储

    # 保存数据到CSV
    def save_sector_data(data, filename):
    data.to_csv(f"{filename
    }.csv", encoding='utf-8-sig')
    # 读取CSV数据
    def load_sector_data(filename):
    return pd.read_csv(f"{filename
    }.csv", encoding='utf-8-sig')

实用功能

1. 板块轮动分析

def analyze_sector_rotation():
"""分析板块轮动情况"""
# 获取所有板块行情
concept_quotes = ak.stock_board_concept_name_em()
industry_quotes = ak.stock_board_industry_name_em()
# 合并数据
all_sectors = pd.concat([
concept_quotes[['板块名称', '涨跌幅', '换手率']],
industry_quotes[['板块名称', '涨跌幅', '换手率']]
])
# 按换手率和涨跌幅分析
return all_sectors.sort_values(['换手率', '涨跌幅'], ascending=[False, False])
# 调用示例
rotation_analysis = analyze_sector_rotation()
print("板块轮动分析:")
print(rotation_analysis.head(10))

2. 板块资金流向分析

def analyze_sector_fund_flow():
"""分析板块资金流向"""
# 获取板块资金流向数据
concept_flow = ak.stock_board_concept_fund_flow_rank_em()
industry_flow = ak.stock_board_industry_fund_flow_rank_em()
print("概念板块资金流向:")
print(concept_flow.head())
print("\n行业板块资金流向:")
print(industry_flow.head())
# 调用示例
analyze_sector_fund_flow()

投资策略建议

  1. 多维度分析

    • 结合多个板块数据源
    • 考虑板块间的关联性
    • 注意行业周期特点
  2. 风险控制

    • 避免过度集中某一板块
    • 关注板块整体估值水平
    • 注意市场情绪变化
  3. 实时监控

    • 设置板块涨跌幅预警
    • 跟踪板块资金流向
    • 关注板块热点转换

相关资源

进阶应用

  1. 自动化监控
  2. 数据可视化
  3. 策略回测
posted @ 2025-09-17 09:51  yfceshi  阅读(964)  评论(0)    收藏  举报