1、判断给定的日期是周几(2种方法):
方法一:
from datetime import datetime
week = datetime.strptime("20191212","%Y%m%d").weekday() # 直接获得周几数据
week = datetime.now().weekday()
方法二:
import calendar
calendar.weekday(2019,12,12) + 1 # 下标从0开始,算周几时需要加1
2、获取某一年每月的日历数据:
calendar_add_list = []
for month in range(1, 13): # 遍历1-12月
week_list = calendar.monthcalendar(year, month) # 按周的格式,获取每月日历
3、获取法定节假日接口
以国务院发布的公告为准,随时调整及增加;http://www.gov.cn/zfwj/bgtfd.htm或http://www.gov.cn/zhengce/xxgkzl.htm
4、获取官方节假日数据:
import calendar
from urllib import request
from random import randrange
import json
import time
year = 2020
data_list = []
for month in range(1,13):
week_list = calendar.monthcalendar(year,month)
for i in week_list:
for j in i:
if j!=0:
# 法定节假日信息
current_date = "%s%02d%02d" % (year, month, j)
# target_url = "http://www.easybots.cn/api/holiday.php?d=" + current_date
# target_url = "http://api.goseek.cn/Tools/holiday?date=" + current_date
target_url = "http://tool.bitefu.net/jiari/?back=json&d=" + current_date
print(target_url)
headers = [
("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"),
("Accept-Encoding", "gzip, deflate"),
("Accept-Language", "zh-CN,zh;q=0.9"),
("Connection", "keep-alive"),
("Host", "api.goseek.cn"),
("Upgrade-Insecure-Requests", 1),
("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36")
]
opener = request.build_opener()
opener.addheaders = headers
while 1:
try:
resp = opener.open(target_url).read()
# resp = request.urlopen(url=target_url).read()
if resp:
break
except Exception as err:
print("----")
pass
print(resp)
data = json.loads(resp)
res = dict()
res["data"] = data[current_date]
res["date"] = current_date
print(res)
# resp = str(resp)[2:-1].replace("\"","'")
data_list.append(res)
time.sleep(randrange(1,5))
data = json.dumps(data_list)
with open("{}cla.json".format(year),"w",encoding="utf-8") as fp:
fp.write(data)
with open("{}cla.json".format(year),"r",encoding="utf-8") as fp:
a = fp.read()
b = json.loads(a)
print(a)
for i in b:
print(i)