由于工作需求,要对几万的数据的日期区分节假日、工作日和周末三种数据。下面是我的心得与代码,有什么问题可以尽管提出来哦。
方法一:
首先,先放一种访问url的代码,这个方法其实很方便,不需要自行手动输入太多。
import json
import requests
import time
#自行设置格式 格式20190225
nowTime = time.strftime('%Y%m%d', time.localtime())
d = '20190720'
# 节假日接口(工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2 )
server_url = "http://www.easybots.cn/api/holiday.php?d="
req = requests.get(server_url + d)
# 获取data值
vop_data = json.loads(req.text)
print('日期 ' + str(d) + '\n查询结果为 ' + str(vop_data) + '\n结论 ', end=' ')
if int(vop_data[d]) == 0:
print('Its weekday')
elif int(vop_data[d]) == 1:
print('Its weekend')
elif int(vop_data[d]) == 2:
print('Its holiday')
else:
print('Error')
但是有个缺点就是:如果你要查询的日期很多,你就会频繁访问网站。。。。会很容易被网站认为你攻击网站。。然后出错。。。。。。。像我。。。。。。。。我的数据是差不多6万这样,访问到3万次的时候。。报错了(如下)
ConnectionError: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None))
所以当你要判断的日期数量不多的时候,就可以使用方法一、当你判断数量太多时,这个方法就不太可行了。当然,有些朋友会说,我可以设置间隔,还可以加什么加什么。。。可能是我比较菜。。我也尝试过增加某些代码。。可是还是不成功,关于间隔,我没有去尝试,就开始写方法二的代码了。
参考:https://blog.csdn.net/iamlvshijie/article/details/72630869
方法二
方法二比较笨拙。。因为我找不到好的方法,只能自己去查询日期自己写代码了。。。发现更好方法的大神烦请你在下面留言、。。跪求简洁明了的方法哈哈哈。
def date_sort(data):
hol = {"2017-01-01", "2017-01-02", "2017-01-27", "2017-01-28", "2017-01-29", "2017-01-30", "2017-01-31",
"2017-02-01","2017-02-02", "2017-04-02", "2017-04-03", "2017-04-04", "2017-04-29", "2017-04-30",
"2017-05-01","2017-05-28", "2017-05-29", "2017-05-30", "2017-10-01", "2017-10-02", "2017-10-03",
"2017-10-04", "2017-10-05", "2017-10-06", "2017-10-07", "2017-10-08", "2017-12-30", "2017-12-31",
"2018-01-01", "2018-02-15", "2018-02-16", "2018-02-17", "2018-02-18", "2018-02-19", "2018-02-20",
"2018-02-21", "2018-04-05", "2018-04-06", "2018-04-07", "2018-04-29", "2018-04-30", "2018-05-01"
"2018-06-16", "2018-06-17", "2018-06-18", "2018-09-22", "2018-09-23", "2018-09-24", "2018-10-01",
"2018-10-02", "2018-10-03", "2018-10-04", "2018-10-05", "2018-10-06", "2018-10-07", "2018-12-30",
"2018-12-31",
"2019-01-01", "2019-02-04", "2019-02-05", "2019-02-06", "2019-02-07", "2019-02-08", "2019-02-09",
"2019-02-10", "2019-04-05", "2019-04-06", "2019-04-07"}
weekend = {"2017-01-07","2017-01-08","2017-01-14","2017-01-15","2017-01-21","2017-02-05","2017-02-11",
"2017-02-12","2017-02-18","2017-02-19","2017-02-25","2017-02-26","2017-03-04","2017-03-05",
"2017-03-11","2017-03-12","2017-03-18","2017-03-19","2017-03-25","2017-03-26","2017-04-08",
"2017-04-09","2017-04-15","2017-04-16","2017-04-22","2017-04-23","2017-05-06","2017-05-07",
"2017-05-13","2017-05-14","2017-05-20","2017-05-21","2017-06-03","2017-06-04","2017-06-10",
"2017-06-11","2017-06-17","2017-06-18","2017-06-24","2017-06-25","2017-07-01","2017-07-02",
"2017-07-08","2017-07-09","2017-07-15","2017-07-16","2017-07-22","2017-07-23","2017-07-29",
"2017-07-30","2017-08-05","2017-08-06","2017-08-12","2017-08-13","2017-08-19","2017-08-20",
"2017-08-26","2017-08-27","2017-09-02","2017-09-03","2017-09-09","2017-09-10","2017-09-16",
"2017-09-17","2017-09-23","2017-09-24","2017-10-14","2017-10-15","2017-10-21","2017-10-22",
"2017-10-28","2017-10-29","2017-11-04","2017-11-05","2017-11-11","2017-11-12","2017-11-18",
"2017-11-19","2017-11-25","2017-11-26","2017-12-02","2017-12-03","2017-12-09","2017-12-10",
"2017-12-16","2017-12-17","2017-12-23","2017-12-24",
"2018-01-06","2018-01-07","2018-01-14","2018-01-13","2018-01-21","2018-01-20","2018-01-27",
"2018-01-28","2018-02-03","2018-02-04","2018-02-10","2018-02-25","2018-03-04","2018-03-03",
"2018-03-11","2018-03-10","2018-03-18","2018-03-17","2018-03-25","2018-03-24","2018-03-31",
"2018-04-01","2018-04-15","2018-04-14","2018-04-22","2018-04-21","2018-05-06","2018-05-05",
"2018-05-13","2018-05-12","2018-05-20","2018-05-19","2018-05-26","2018-05-27","2018-06-03",
"2018-06-02","2018-06-10","2018-06-09","2018-06-24","2018-06-23","2018-06-30","2018-07-01",
"2018-07-02","2018-07-08","2018-07-07","2018-07-15","2018-07-14","2018-07-22","2018-07-21",
"2018-07-29","2018-07-28","2018-08-05","2018-08-04","2018-08-12","2018-08-11","2018-08-19",
"2018-08-18","2018-08-26","2018-08-25","2018-09-02","2018-09-01","2018-09-09","2018-09-08",
"2018-09-16","2018-09-15","2018-10-14","2018-10-13","2018-10-21","2018-10-20","2018-10-28",
"2018-10-27","2018-11-04","2018-11-03","2018-11-11","2018-11-10","2018-11-18","2018-11-17",
"2018-11-25","2018-11-24","2018-12-02","2018-12-01","2018-12-09","2018-12-08","2018-12-16",
"2018-12-15","2018-12-23","2018-12-22",
"2019-01-06","2019-01-05","2019-01-12","2019-01-13","2019-01-19","2019-01-20","2019-01-27",
"2019-01-26","2019-02-16","2019-02-17","2019-02-23","2019-02-24","2019-03-02","2019-03-03",
"2019-03-09","2019-03-10","2019-03-16","2019-03-17","2019-03-23","2019-03-24","2019-03-31",
"2019-03-30","2019-04-13","2019-04-14","2019-04-20","2019-04-21","2019-04-27"}
date_list = []
weeke = 'weekend'
holiday = 'holiday'
weekday = 'weekday'
for i in range(len(data)):
d = data[i]
d = year+ '-' + month+ '-' +day#按照自己的日期格式改成以上形式
if d in hol :
date_list.append(holiday)
elif d in weekend :
date_list.append(weeke)
else :date_list.append(weekday)
return date_list
在倒数第7行那里,要把自己的日期改成上面的形式,下面举个例子。假设我的日期格式是09/24/2018
>>> date = '09/24/2018'
>>> d = date[6:10] + '-' + date[0:2] + '-' + date[3:5]
>>> d
'2018-09-24'
自己查询真的是有点麻烦的。。不知道能不能找到更好的方法哈哈哈。有更好的方法在下面留言哦!!!!万分感谢