#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"
# 因该接口传入的时间,查询了前一个月,当前月和后一个月的数据,所以只需要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)
打印结果如图
其中部分是参考这位大佬的帖子
链接: [link]https://blog.csdn.net/laow1314/article/details/121906624