Python:使用chinesecalendar获取中国节假日判断工作日和节假日

您所在的位置:网站首页 一年多少节假日和周末一样 Python:使用chinesecalendar获取中国节假日判断工作日和节假日

Python:使用chinesecalendar获取中国节假日判断工作日和节假日

2024-07-14 18:56:22| 来源: 网络整理| 查看: 265

目录 现有方案:chinesecalendar基于网络的方案使用china-calendar

现有方案:chinesecalendar

使用chinesecalendar库可以实现:判断一天是不是法定节假日/法定工作日(查看节假日安排)

https://pypi.org/project/chinesecalendar/https://github.com/LKI/chinese-calendar/

安装

pip install chinesecalendar

使用示例

import datetime # 判断 2018年4月30号 是不是节假日 from chinese_calendar import is_holiday, is_workday april_last = datetime.date(2018, 4, 30) assert is_workday(april_last) is False

缺点:

1、更新不及时,或需要手动更新才能判断,由于次年的节假日安排,取决于国务院发布的日程。 所以本项目一般会在国务院更新以后,发布新的版本。 按照以往的经验,一般是每年的 11月 前后发布新版本。2、支持的版本有点高,Python :: 3.8起步,不支持低版本 基于网络的方案

可以使用现有的维护的比较好的网站数据,比如:https://wannianrili.bmcx.com/,通过网页请求解析获取日历数据,这样就能获取到实时的数据,缺点也很明显,需要网络请求,如果考虑性能的场景下,可能不适用。

依赖两个第三方库:

pip install requests parsel

代码文件,可以放到项目的utils工具包内

# -*- coding: utf-8 -*- """ @File : calendar_util.py @Date : 2024-04-03 """ import json import re import requests from parsel import Selector def get_calendar(year_and_month): """ 数据来源:https://wannianrili.bmcx.com/ @return: { "2024-04-01": { "class_name": "", "comment": "愚人节" } } """ # https://wannianrili.bmcx.com/ajax/?q=2024-04&v=22121303 url = 'https://wannianrili.bmcx.com/ajax/' params = { 'q': year_and_month, 'v': '22121303' } headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0" } res = requests.get(url, params=params, headers=headers) sel = Selector(text=res.text) rows = sel.css('.wnrl_riqi') data = {} for row in rows: class_name = row.css('a::attr(class)').extract_first('').strip() day = row.css('a::attr(onclick)').extract_first('').strip() comment = row.css('.wnrl_td_bzl::text').extract_first('').strip() ret = re.search('\d{4}-\d{2}-\d{2}', day) day = ret.group(0) data[day] = { 'class_name': class_name, 'comment': comment } return data def get_day_item(day): """ @param day: @return: { "class_name": "", "comment": "愚人节" } """ # 2024-05-04 => 2024-05 calendar = get_calendar('-'.join(day.split('-')[:2])) # print(json.dumps(calendar, indent=2, ensure_ascii=False)) return calendar.get(day) def is_workday(day): # 工作日 workday_class_list = ['', 'wnrl_riqi_ban'] day_item = get_day_item(day) if day_item: if day_item.get('class_name') in workday_class_list: return True else: return False def is_holiday(day): # 节假日 holiday_class_list = ['wnrl_riqi_xiu', 'wnrl_riqi_mo'] day_item = get_day_item(day) if day_item: if day_item.get('class_name') in holiday_class_list: return True else: return False if __name__ == '__main__': print(is_holiday('2024-05-04')) # True 使用china-calendar

基于以上代码,作者封装了一个库,可以直接使用

文档:

https://github.com/mouday/china-calendarhttps://pypi.org/project/china-calendar/

安装

pip install china-calendar

使用示例:

import china_calendar # 清明节 assert china_calendar.is_holiday('2024-04-04') == True assert china_calendar.is_workday('2024-04-04') == False


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭