其他文章/不再维护

股票代码分类的Python代码

这是之前的一篇:使用AKShare库获取股票数据。本篇根据获取到数据,对所有的股票代码进行分类。需要用到AKShare库,安装命令:pip install akshare --upgrade。

说明:这里只是按照股票代码进行的分类,有些可能是待上市的公司或者是其他的情况,因此可能和别处给的统计数据不一定完全对得上,但基本上差不多,仅供参考。

Python代码如下:

"""
This code is supported by the website: https://www.guanjihuan.com
The newest version of this code is on the web page: https://www.guanjihuan.com/archives/38180
"""

import akshare as ak
import re

stocks = ak.stock_zh_a_spot_em()
stock_data = stocks.values
stock_symbols = stock_data[:, 1]
num_stocks = len(stock_symbols)
print('所有股票数量:', num_stocks)
# print(stock_symbols)

# 上交所主板
stock_symbols_60 = []
for stock_symbol in stock_symbols:
    find_600 = re.findall(r'^600', stock_symbol)
    find_601 = re.findall(r'^601', stock_symbol)
    find_603 = re.findall(r'^603', stock_symbol)
    find_605 = re.findall(r'^605', stock_symbol)
    if find_600 != [] or find_601 != [] or find_603 != [] or find_605 != []:
        stock_symbols_60.append(stock_symbol)
num_stocks_60 = len(stock_symbols_60)
print('上交所主板股票数量:', num_stocks_60)
# print(stock_symbols_60)

# 深交所主板
stock_symbols_00 = []
for stock_symbol in stock_symbols:
    find_000 = re.findall(r'^000', stock_symbol)
    find_001 = re.findall(r'^001', stock_symbol)
    find_002 = re.findall(r'^002', stock_symbol)
    find_003 = re.findall(r'^003', stock_symbol)
    if find_000 != [] or find_001 != [] or find_002 != [] or find_003 != []:
        stock_symbols_00.append(stock_symbol)
num_stocks_00 = len(stock_symbols_00)
print('深交所主板股票数量:', num_stocks_00)
# print(stock_symbols_00)

# 创业板
stock_symbols_30 = []
for stock_symbol in stock_symbols:
    find_300 = re.findall(r'^300', stock_symbol)
    find_301 = re.findall(r'^301', stock_symbol)
    if find_300 != [] or find_301 != []:
        stock_symbols_30.append(stock_symbol)
num_stocks_30 = len(stock_symbols_30)
print('创业板股票数量:', num_stocks_30)
# print(stock_symbols_30)

# 科创板
stock_symbols_68 = []
for stock_symbol in stock_symbols:
    find_688 = re.findall(r'^688', stock_symbol)
    find_689 = re.findall(r'^689', stock_symbol)
    if find_688 != [] or find_689 != []:
        stock_symbols_68.append(stock_symbol)
num_stocks_68= len(stock_symbols_68)
print('科创板股票数量:', num_stocks_68)
# print(stock_symbols_68)

# 新三板
stock_symbols_8_4 = []
for stock_symbol in stock_symbols:
    find_82 = re.findall(r'^82', stock_symbol)
    find_83 = re.findall(r'^83', stock_symbol)
    find_87 = re.findall(r'^87', stock_symbol)
    find_88 = re.findall(r'^88', stock_symbol)
    find_430 = re.findall(r'^430', stock_symbol)
    find_420 = re.findall(r'^420', stock_symbol)
    find_400 = re.findall(r'^400', stock_symbol)
    if find_82 != [] or find_83 != [] or find_87 != [] or find_88 != [] or find_430 != [] or find_420 != [] or find_400 != []:
        stock_symbols_8_4.append(stock_symbol)
num_stocks_8_4= len(stock_symbols_8_4)
print('新三板股票数量:', num_stocks_8_4)
# print(stock_symbols_8_4)

print('所有股票数量:', num_stocks_60+num_stocks_00+num_stocks_30+num_stocks_68+num_stocks_8_4)

# 检查遗漏的股票代码
for stock_symbol in stock_symbols:
    if stock_symbol not in stock_symbols_60 and stock_symbol not in stock_symbols_00 and stock_symbol not in stock_symbols_30 and stock_symbol not in stock_symbols_68 and stock_symbol not in stock_symbols_8_4:
        print(stock_symbol)

2023年12月28日运行的结果为:

所有股票数量: 5575
上交所主板股票数量: 1797
深交所主板股票数量: 1611
创业板股票数量: 1359
科创板股票数量: 569
新三板股票数量: 239
所有股票数量: 5575
93 次浏览

【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code