用python获取全年的节假日和补班日

废话不多说,上代码:

先通过百度的日历接口,获取日历数据

url = f"https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={year}年{month}月&co=&resource_id=39043&t=1617089428269&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=wisetpl&cb=jQuery110203576901702188473_1617089118772&_=1617089118776"

完整代码: 新建一个py文件:get_on_duty_days

import requests
import json

def catch_url_form_baidu(year, month):
    url = f"https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={year}年{month}月&co=&resource_id=39043&t=1617089428269&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=wisetpl&cb=jQuery110203576901702188473_1617089118772&_=1617089118776"
    r = requests.get(url).text

    start_index = r.find("(")
    r = r[start_index + 1:len(r) - 2]

    month_data = json.loads(r)
    data_value = month_data["data"]
    # 转成数组
    data_array = json.loads(data_value) if isinstance(data_value, str) else data_value
    obj = data_array[0]

    almanac = obj["almanac"]

    holiday = []
    on_duty = []
    almanac_array = json.loads(almanac) if isinstance(almanac, str) else almanac
    for item in almanac_array:
        data_json = item
        # 只取年月日
        day_str = data_json["oDate"][:10]
        # 这里用 .get 方法,如果有存在 key,就取,不存在则赋值为 0,最终转成 int
        status = int(data_json.get("status", 0))
        if status == 2:
            on_duty.append(day_str)
        if status == 1:
            holiday.append(day_str)
    return holiday, on_duty

再建一个文件,写个方法调用它

import get_on_duty_days

print("begin")

holiday_year = []
on_duty_year = []

year = 2024
for month in range(1, 13):
    holiday, on_duty = get_on_duty_days.catch_url_form_baidu(year, month)
    holiday_year.extend(holiday)
    on_duty_year.extend(on_duty)

print(holiday_year)
print(on_duty_year)

最终打印:

['2023-12-29', '2023-12-30', '2023-12-31', '2024-02-09', '2024-02-10', '2024-02-11', '2024-02-12', '2024-02-13', '2024-02-14', '2024-02-15', '2024-02-16', '2023-12-31', '2024-02-09', '2024-02-10', '2024-02-11', '2024-02-12', '2024-0
2-13', '2024-02-14', '2024-02-15', '2024-02-16', '2024-02-09', '2024-02-10', '2024-02-11', '2024-02-12', '2024-02-13', '2024-02-14', '2024-02-15', '2024-02-16', '2024-04-03', '2024-04-04', '2024-04-05', '2024-04-03', '2024-04-04', '
2024-04-05', '2024-04-30', '2024-05-01', '2024-05-02', '2024-05-03', '2024-05-04', '2024-04-03', '2024-04-04', '2024-04-05', '2024-04-30', '2024-05-01', '2024-05-02', '2024-05-03', '2024-05-04', '2024-06-07', '2024-06-08', '2024-06-
09', '2024-04-30', '2024-05-01', '2024-05-02', '2024-05-03', '2024-05-04', '2024-06-07', '2024-06-08', '2024-06-09', '2024-06-07', '2024-06-08', '2024-06-09', '2024-09-14', '2024-09-15', '2024-09-16', '2024-09-14', '2024-09-15', '20
24-09-16', '2024-09-30', '2024-10-01', '2024-10-02', '2024-10-03', '2024-10-04', '2024-10-05', '2024-10-06', '2024-09-14', '2024-09-15', '2024-09-16', '2024-09-30', '2024-10-01', '2024-10-02', '2024-10-03', '2024-10-04', '2024-10-05
', '2024-10-06', '2024-09-30', '2024-10-01', '2024-10-02', '2024-10-03', '2024-10-04', '2024-10-05', '2024-10-06']


['2024-02-03', '2024-02-17', '2024-02-03', '2024-02-17', '2024-02-03', '2024-02-17', '2024-04-06', '2024-04-27', '2024-04-06', '2024-04-27', '2024-05-10', '2024-04-06', '2024-04-27', '2024-05-10', '2024-05-10', '2024-09-13', '2024-0
9-28', '2024-09-13', '2024-09-28', '2024-10-11', '2024-09-13', '2024-09-28', '2024-10-11', '2024-10-11']
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值