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

devtools/2024/10/20 5:46:40/

0x00 前言

由于此前从深交所下载的股票信息中只有行业门类信息,没有行业大类信息,导致后续解析三大报表和量化选股的时候无法进行:
在这里插入图片描述
可以看到深交所的股票是没有大类信息的。
再看看上交所的保险股:
在这里插入图片描述
因此需要将深交所股票的所属大类信息也添加上。
这里可以直接使用中国上市公司协会每隔一段时间发布的《上市公司行业分类结果》。
目前最新版本是《2023年下半年上市公司行业分类结果》
在这里插入图片描述
具体的解析逻辑不再赘述,分析一下HTML的格式就能把最新的pdf拿到手来解析。
直接上爬虫代码:

import osimport 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

然后控制器那边这样写:

# 更新上市公司所属行业门类及大类
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("上市公司行业分类更新结束.")

然后深交所的行业就可以补齐了:
在这里插入图片描述


http://www.ppmy.cn/devtools/100380.html

相关文章

位数问题c++

题目描述 在所有的N位数中&#xff0c;有多少个数中有偶数个数字3&#xff0c;由于结果可能很大&#xff0c;你只需要输出这个答案对12345取余的值。 输入 读入一个数N(N≤1000) 输出 输出有多少个数中有偶数个数字3。 样例输入 2 样例输出 73样例解释&#xff1a; 1…

Spring Boot与桥接模式:构建灵活的产品分类体系

在当今的软件开发领域&#xff0c;特别是在构建大型应用时&#xff0c;模块化和灵活性成为了至关重要的设计原则。Spring Boot&#xff0c;以其便捷的开发体验和强大的生态支持&#xff0c;成为许多开发者首选的Java开发框架之一。本文将探讨如何利用Spring Boot结合桥接模式来…

上半年大模型遍地开花,大模型发展中有哪些经验和教训?

前言 过去一年里&#xff0c;大模型遍地开花&#xff0c;我自己也在做大模型训练相关的工作&#xff0c;踩过了很多很多坑&#xff0c;这里分享一些教训&#xff1a;用成熟的分布式训练框架&#xff1a; 多用 DeepSpeed&#xff0c;少用 Pytorch 原生的 torchrun。在节点数量较…

【Ansible】Ansible playbook

Ansible playbook简介 Ansible playbook是一种用于描述和自动化IT基础设施配置和管理的工具。它使用YAML格式来定义一系列任务和配置项&#xff0c;并利用Ansible的执行引擎自动执行这些任务。 Playbook包含一个或多个play&#xff0c;每个play定义了一组任务&#xff0c;这些…

NLP -->定义、应用、与职业前景解析

1. 自然语言处理&#xff08;NLP&#xff09;的定义与误区 定义: 自然语言处理主要集中于文本的处理&#xff0c;旨在使计算机能够理解和生成自然语言文本。常见误区: 初学者容易将自然语言处理与语音识别混淆。 语音识别: 将语音转成文字&#xff0c;这不属于自然语言处理范畴…

使用Python请求http/https时如何设置失败重试次数例子解析

代码示例&#xff1a; import requests from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.retry import Retrydef requests_with_retries_example():# 创建一个session对象&#xff0c;该对象将被配置为在失败时自动重试session requests.Ses…

VirtualBox下安装Centos7.9虚拟机的踩坑记录

目录 0 背景1 安装Centos7.91.1 下载iso镜像1.2 正常安装虚拟机1.3 将用户添加到sudoers并免密1.4 更新yum源1.5 配置静态IP1.6 通过ssh工具传文件1.7 总结 0 背景 最近搞了个便宜的低配台式机用来敲代码&#xff0c;主要是嫌弃笔记本屏幕太小了&#xff0c;想用个大屏。 然后我…

torch.max()学习记录

xtourch.tensor([[1,2,3],[4,6,5],[9,11,4],[-2,6,20]])为4*3的一个张量 1)请思考y的值 ytorch.max(x,dim0) 分析&#xff1a;x是一个2D: 4*3的张量&#xff0c;dim0 &#xff0c;表示按照行操作&#xff0c;得到的张量shape3,对应的是[1,4,9,-2】&#xff0c;【2,6,11,6】…