Akshare获取同花顺行业

使用akshare可以很方便的获取同花顺行业列表,与每个行业对应的个股信息,流程如下:

  1. 使用ak.stock_board_industry_summary_ths()获取行业列表
  2. 循环行业列表,使用ak.stock_board_industry_cons_ths()获取行业对应的个股信息

官方文档请参考:https://akshare.akfamily.xyz/data/stock/stock.html#id332

示例代码

py 复制代码
import os
import pandas as pd
import time
from tqdm import tqdm
import akshare as ak


class THSIndustry(object):
    """同花顺行业"""

    def __init__(self, concept_file):
        self.concept_file = concept_file

    def update(self):
        stock_industry_df = ak.stock_board_industry_summary_ths()  # 同花顺
        industry_list = []
        for concept_dict_data in tqdm(stock_industry_df.to_dict(orient="records"), desc="更新同花顺行业"):
            time.sleep(3)
            stock_board_concept_df = ak.stock_board_industry_cons_ths(symbol=concept_dict_data['板块'])
            stock_board_concept_df['行业'] = concept_dict_data['板块']
            [industry_list.append(_) for _ in stock_board_concept_df.to_dict(orient="records")]
        # 导出结果
        pd.DataFrame(industry_list).to_csv(self.concept_file)

    def get_industry_df(self):
        if not os.path.exists(self.concept_file):
            return None
        industry_df = pd.read_csv(self.concept_file, index_col=0)
        industry_df.reset_index(inplace=True, drop=True)
        return industry_df


def main():
    industry_info = THSIndustry("ths_concept.csv")
    industry_info.update() # 更新列表
    industry_df = industry_info.get_industry_df() # 获取行业全列表


if __name__ == '__main__':
    main()

运行上面的代码,最后的industry_df即为全行业信息及对应的个股:

相关推荐
CryptoPP1 天前
快速对接东京证券交易所API数据:实战指南与代码示例
开发语言·人工智能·windows·python·信息可视化·区块链
ZC跨境爬虫1 天前
跟着 MDN 学JavaScript day_7:数学运算与逻辑判断实战测试
开发语言·前端·javascript·学习·ecmascript
探物 AI1 天前
把 MambaOut 塞进 YOLOv11:会有什么样的反应
python·yolo·计算机视觉
如竟没有火炬1 天前
最大矩阵——单调栈
数据结构·python·线性代数·算法·leetcode·矩阵
阳区欠1 天前
【LangChain】LLM基础介绍
开发语言·python·langchain
Cosolar1 天前
保姆级 CrewAI 教程:从零构建多智能体协作系统
人工智能·python·架构
Jinkxs1 天前
Java 跨域14-Java 与区块链(Hyperledger)集成
java·开发语言·区块链
GDAL1 天前
使用 uv 管理 Python 版本
python·uv·版本
真实的菜1 天前
Redis 从入门到精通(十二):典型业务场景实战 —— 排行榜、限流器、秒杀系统、Session 共享
数据库·redis·python
cup111 天前
[开源] Meta Assistant / 告别命令行,我为一堆 Python 脚本做了一个 Windows 任务栏的“家”
windows·python·工具·nuitka·脚本运行