爬取百度日历2022年节假日信息

#2022年放假计划

###本文实现通过对百度日历接口的爬取,获取做休信息,并对数据进行进一步处理

下面展示一些 内联代码片

import requests
import json
import random
import datetime

dataTime = str(random.random())
# 从百度的php接口中获取到数据
def catch_url_from_baidu(calcultaion_year, month):
    header = {
        "Content-Type": "application/json;charset=UTF-8"
    }
    param = {
        "query": calcultaion_year + "年" + month + "月",
        "resource_id": "39043",
        "t": dataTime,
        "ie": "utf8",
        "oe": "gbk",
        "format": "json",
        "tn": "wisetpl",
        "cb": ""
    }
    # 抓取位置:百度搜索框搜索日历,上面的日历的接口,可以在页面上进行核对
    r = requests.get(url="https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php",
                     headers=header, params=param).text
    month_data = json.loads(r)["data"][0]["almanac"]

    for one in month_data:      # 偶数上班,奇数休息
        # 非周末 且 无status 为正常工作日,记作:0
        if ((one["cnDay"] != '日' and one["cnDay"] != '六') and 'status' not in one):
            v = datetime.date(int(one['year']),int(one['month']),int(one['day']))
            print(v,'\t0')

        # 周末 且 无status 为正常周末休息,记作:1
        elif ((one["cnDay"] == '日' or one["cnDay"] == '六') and 'status' not in one):
            v = datetime.date(int(one['year']),int(one['month']),int(one['day']))
            print(v,'\t1')

        # 周末 且 status=2(补班) 为周末补班,记作:2
        elif ((one["cnDay"] == '日' or one["cnDay"] == '六') and ('status'  in one and one["status"] == '2')):
            v = datetime.date(int(one['year']),int(one['month']),int(one['day']))
            print(v,'\t2')

        # status=1(节假日) 为节假日休息,记作:3
        elif ( 'status'  in one and one["status"] == '1' ):
            v = datetime.date(int(one['year']),int(one['month']),int(one['day']))
            print(v,'\t3')


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


打印结果如图
在这里插入图片描述

其中部分是参考这位大佬的帖子
链接: [link]https://blog.csdn.net/laow1314/article/details/121906624

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值