We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
原文地址:https://blog.csdn.net/mouday/article/details/137352491
@TOC
使用chinesecalendar库可以实现:判断一天是不是法定节假日/法定工作日(查看节假日安排)
安装
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
缺点:
Python :: 3.8
可以使用现有的维护的比较好的网站数据,比如:https://wannianrili.bmcx.com/,通过网页请求解析获取日历数据,这样就能获取到实时的数据,缺点也很明显,需要网络请求,如果考虑性能的场景下,可能不适用。
依赖两个第三方库:
pip install requests parsel
代码文件,可以放到项目的utils工具包内
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
The text was updated successfully, but these errors were encountered:
我把上面的代码,简单的封装了一个库,分享给大家
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
Sorry, something went wrong.
我把上面的代码,简单的封装了一个库,分享给大家 安装 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
2025-01-28 为什么检查是False
No branches or pull requests
原文地址:https://blog.csdn.net/mouday/article/details/137352491
@TOC
现有方案:chinesecalendar
使用chinesecalendar库可以实现:判断一天是不是法定节假日/法定工作日(查看节假日安排)
安装
使用示例
缺点:
Python :: 3.8
起步,不支持低版本基于网络的方案
可以使用现有的维护的比较好的网站数据,比如:https://wannianrili.bmcx.com/,通过网页请求解析获取日历数据,这样就能获取到实时的数据,缺点也很明显,需要网络请求,如果考虑性能的场景下,可能不适用。
依赖两个第三方库:
代码文件,可以放到项目的
utils
工具包内The text was updated successfully, but these errors were encountered: