学海无涯,旅“途”漫漫,“途”中小记,如有错误,敬请指出,在此拜谢!

前言

最近在工作的业务中,需要对工作日进行判别(比如某件事情计算五个工作日之后的时间,去掉节假日和双休周末)。百度查了很多资料,没有发现什么api之类的接口,大部分会建议本地建表来进行非工作日的维护。查了好多github和gitee上的代码,看得我一脸懵逼。我这暴脾气就不开心了,自己写一套吧。闲话不多说,上代码。有什么问题的筒子们请在评论区留言。

代码

# coding=utf-8
# !/usr/bin/python
import requests
import json


# 从百度的php接口中获取到数据
def catch_url_from_baidu(calcultaion_year, month):
    headers = {
        "Content-Type": "application/json;charset=UTF-8"
    }
    param = {
        "query": calcultaion_year + "年" + month + "月",
        "resource_id": "39043",
        "t": "1604395059555",
        "ie": "utf8",
        "oe": "gbk",
        "format": "json",
        "tn": "wisetpl",
        "cb": ""
    }
    # 抓取位置:百度搜索框搜索日历,上面的日历的接口,可以在页面上进行核对
    r = requests.get(url="https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php",
                     headers=headers, params=param).text
    month_data = json.loads(r)["data"][0]["almanac"]
    not_work_day = []
    for one in month_data:
        if (one["cnDay"] == '日' or one["cnDay"] == '六'):
            if ('status' in one):
                if (one["status"] == "2"):
                    # status为2的时候表示周末的工作日,比如10月10日。即百度工具左上角显示“班”的日期
                    continue
                else:
                    # 普通周末时间
                    not_work_day.append(one)
                    continue
            else:
                # 普通周末时间。(接口中,如果左上角没有特殊表示,则不会返回status)
                not_work_day.append(one)
                continue
        if ('status' in one and one["status"] == "1"):
            # status为1的时候表示休息日,比如10月1日。即百度工具左上角显示“休”的日期
            not_work_day.append(one)
    print_info(not_work_day)


def print_info(not_work_day):
    for one in not_work_day:
        insert_sql = "insert TABLE (YEAR,MONTH,DAY) VALUES (" + one["year"] + "," + one[
            "month"] + "," + one["day"] + ");"
        print(insert_sql)
        # print("月:" + one["month"] + "日:" + one["day"])


if __name__ == '__main__':
    # 此处只能算当年之前的,因为国务院是每年12月份才会发布第二年的放假计划,所以此接口对于下一年的统计是错的。eg:2020年11月4日,国务院没有发布21年的放假计划,那查询2021年元旦的时候,元旦那天不显示休息
    calcultaion_year = "2022"
    # 因该接口传入的时间,查询了前一个月,当前月和后一个月的数据,所以只需要2、5、8、11即可全部获取到。比如查询5月份,则会查询4,5,6月分的数据
    calculation_month = ["2", "5", "8", "11"]
    for one_month in calculation_month:
        catch_url_from_baidu(calcultaion_year, one_month)

输出结果(23年所有非工作日内容)

注意,此处是所有非工作日内容,你把365天排除掉这些日子,就是所有工作日天数了

insert TABLE (YEAR,MONTH,DAY) VALUES (2023,1,1);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,1,2);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,1,7);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,1,8);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,1,14);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,1,15);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,1,21);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,1,22);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,1,23);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,1,24);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,1,25);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,1,26);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,1,27);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,2,4);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,2,5);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,2,11);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,2,12);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,2,18);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,2,19);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,2,25);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,2,26);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,3,4);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,3,5);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,3,11);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,3,12);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,3,18);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,3,19);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,3,25);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,3,26);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,4,1);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,4,2);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,4,5);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,4,8);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,4,9);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,4,15);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,4,16);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,4,22);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,4,29);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,4,30);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,5,1);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,5,2);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,5,3);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,5,7);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,5,13);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,5,14);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,5,20);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,5,21);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,5,27);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,5,28);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,6,3);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,6,4);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,6,10);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,6,11);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,6,17);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,6,18);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,6,22);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,6,23);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,6,24);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,7,1);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,7,2);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,7,8);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,7,9);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,7,15);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,7,16);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,7,22);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,7,23);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,7,29);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,7,30);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,8,5);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,8,6);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,8,12);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,8,13);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,8,19);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,8,20);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,8,26);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,8,27);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,9,2);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,9,3);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,9,9);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,9,10);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,9,16);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,9,17);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,9,23);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,9,24);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,9,29);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,9,30);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,10,1);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,10,2);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,10,3);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,10,4);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,10,5);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,10,6);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,10,14);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,10,15);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,10,21);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,10,22);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,10,28);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,10,29);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,11,4);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,11,5);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,11,11);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,11,12);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,11,18);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,11,19);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,11,25);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,11,26);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,12,2);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,12,3);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,12,9);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,12,10);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,12,16);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,12,17);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,12,23);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,12,24);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,12,30);
insert TABLE (YEAR,MONTH,DAY) VALUES (2023,12,31);

经验证,问题不大

注意事项

1.本抓包是从如图所示的接口进行抓包的,抓完之后可以验证下。
在这里插入图片描述
2.还是要强调一点:经验证,国家每年11-12月才会发布对应的第二年的放假安排,所以如果还没发布,你抓后面几年的时间是不行的哈!!这样你就没有法定假日了。当然,节假日还要加班的另算!
3.如果哥们你是996计算方案,请更改catch_url_from_baidu方法里面的for循环的if语句就行了。如果兄弟你是007,请print(“365天全年加班”)即可,当然闰年是366天,注意别忘了。

源码

源码我放在了码云上,各位可以瞅瞅

https://gitee.com/doubletreelin/my-python-tools
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐