搭建自己的金融数据源和量化分析平台(七):定时更新上市公司所属行业门类及大类

0x00 前言

由于此前从深交所下载的股票信息中只有行业门类信息,没有行业大类信息,导致后续解析三大报表和量化选股的时候无法进行:

可以看到深交所的股票是没有大类信息的。

再看看上交所的保险股:

因此需要将深交所股票的所属大类信息也添加上。

这里可以直接使用中国上市公司协会每隔一段时间发布的《上市公司行业分类结果》。

目前最新版本是《2023年下半年上市公司行业分类结果》

具体的解析逻辑不再赘述,分析一下HTML的格式就能把最新的pdf拿到手来解析。

直接上爬虫代码:

python 复制代码
import os

import pdfplumber
import requests
from lxml import etree


'''中国上市公司协会的爬虫,读取和解析最新上市公司行业分类结果 返回格式为:股票代码:[一级行业代码,二级行业代码]。举例如下
{
    "stock_code1":[industry,industry_2],
    "stock_code2":[industry,industry_2]
}
'''
def get_A_industry_list():
    basic_url = 'https://www.capco.org.cn/pub/zgssgsxh/xhgg/hyfl/hyfljg/index.html'
    mid_url = 'https://www.capco.org.cn/pub/zgssgsxh/xhgg/hyfl/hyfljg/'
    cache_file_path = "./corporation_category.pdf"
    response = requests.get(basic_url)
    response.encoding = 'UTF-8'
    href_cut = etree.HTML(response.text).xpath(".//div[@class='fr listCon']/h3/a")
    response.close()
    href_mid = etree.tostring(element_or_tree=href_cut[0], encoding='utf-8').decode('utf-8')
    latest_result = href_mid.split("<a href=\"")[1].split("\">")[0].split("./")[1]
    response = requests.get(mid_url+latest_result)
    response.encoding = 'UTF-8'
    href_cut = etree.HTML(response.text).xpath(".//a[@style='font-size:12px; color:#0066cc;']")
    response.close()
    pdf_url_mid = etree.tostring(element_or_tree=href_cut[0], encoding='utf-8').decode('utf-8')
    pdf_url = pdf_url_mid.split("href=\"")[1].split("\" title=\"")[0]
    response = requests.get(pdf_url)
    open(cache_file_path, "wb").write(response.content)
    response.close()
    result = {}
    with pdfplumber.open(cache_file_path) as pdf:
        for page in pdf.pages:
            tables = page.extract_tables()
            for table in tables:
                for line in table:
                    if line[0].find("上市公司") < 0:
                        result[line[0]] = [line[2], line[2]+line[6]]
    os.remove(cache_file_path)
    return result

然后控制器那边这样写:

python 复制代码
# 更新上市公司所属行业门类及大类
def update_A_corporation_category():
    database = "stock_a"
    select_sql = "SELECT stock_code,industry,industry_2 FROM stock_list"
    update_sql = "update stock_list set industry=%s,industry_2=%s where stock_code=%s"
    update_rows = []
    category = get_A_industry_list()
    select_result = ExecSelect(database, select_sql)  # 读取查询结果
    for stock in select_result:
        if stock[2] is None:
            try:
                update_rows.append((category[stock[0]][0], category[stock[0]][1], stock[0]))
            except KeyError:
                print(stock[0], "暂无大类分类结果")
                continue
    # 更新数据库中存在的股票信息
    if len(update_rows) > 0:
        result = ExecInsert(database, update_sql, update_rows)
        if result == 'success':
            print("更新上市公司行业分类成功.")
        else:
            raise CustomException("更新上市公司行业分类时发生数据库异常:" + result)
    print("上市公司行业分类更新结束.")

然后深交所的行业就可以补齐了:

相关推荐
阿坤带你走近大数据10 小时前
什么是元数据管理?(附具体实施方案供参考)
数据库·金融
人间打气筒(Ada)18 小时前
[鸿蒙2025领航者闯关]星盾护航支付安全:鸿蒙6.0在金融APP中的实战闯关记
安全·金融·harmonyos·#鸿蒙2025领航者闯关#·#鸿蒙6实战#·#开发者年度总结#
元宇宙时间2 天前
数字人民币助力亚太经合新金融秩序——构建亚太数字经济与区域金融协同的关键基础设施
大数据·人工智能·金融
2501_921649492 天前
如何获取美股实时行情:Python 量化交易指南
开发语言·后端·python·websocket·金融
数据猿3 天前
【金猿CIO展】莱商银行信息科技部总经理张勇:AI Infra与Data Agent驱动金融数据价值新十年
人工智能·金融
雷焰财经3 天前
从“金桂奖”看金融创新:中和农信如何为乡村振兴引来金融“活水”
金融
低调电报3 天前
技术王者局・鸿蒙 6.0 特性实战闯关:金融级应用安全与异构设备协同开发复盘
安全·金融·harmonyos
雷焰财经3 天前
引金融“活水”精准滴灌,筑乡村振兴坚实根基——中和农信的普惠金融实践
金融
中电金信3 天前
中电金信:智能辅助审单方案让跨境金融审核又快又准
大数据·金融
菊风 Juphoon3 天前
赋能金融租赁,菊风中标浙银金租视频双录系统项目,打造金融租赁合规运营范式
金融