【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《基于Python的日历api调用代码实例》,欢迎阅读!
基于Python的日历api调用代码实例
代码描述:基于Python的日历api调用代码实例 代码平台:聚合数据
#!/usr/bin/python
# -*- coding: utf-8 -*- import json, urllib
from urllib import urlencode
#----------------------------------
# 万年历调用示例代码 - 聚合数据
# 在线接口文档:http://www.juhe.cn/docs/177 #----------------------------------
def main():
#配置您申请的APPKey
appkey = "*********************"
#1.获取当天的详细信息 request1(appkey,"GET")
#2.获取当月近期假期
request2(appkey,"GET")
#3.获取当年的假期列表 request3(appkey,"GET")
#获取当天的详细信息
def request1(appkey, m="GET"):
url = "http://japi.juhe.cn/calendar/day" params = {
"key" : appkey, #您申请的appKey
"date" : "", #指定日期,格式为YYYY-MM-DD,如月份和日期小于10,则取个位,如:2012-1-1
}
params = urlencode(params) if m =="GET":
f = urllib.urlopen("%s?%s" % (url, params)) else:
f = urllib.urlopen(url, params)
content = f.read()
res = json.loads(content) if res:
error_code = res["error_code"] if error_code == 0:
#成功请求
print res["result"] else:
print "%s:%s" % (res["error_code"],res["reason"]) else:
print "request api error"
#获取当月近期假期
def request2(appkey, m="GET"):
url = "http://japi.juhe.cn/calendar/month" params = {
"key" : appkey, #您申请的appKey
"year-month" : "", #指定月份,格式为YYYY-MM,如月份和日期小于10,则取个位,如:2012-1
}
params = urlencode(params) if m =="GET":
f = urllib.urlopen("%s?%s" % (url, params)) else:
f = urllib.urlopen(url, params)
content = f.read()
res = json.loads(content) if res:
error_code = res["error_code"] if error_code == 0:
#成功请求
print res["result"] else:
print "%s:%s" % (res["error_code"],res["reason"]) else:
print "request api error"
#获取当年的假期列表
def request3(appkey, m="GET"):
url = "http://japi.juhe.cn/calendar/year" params = {
"key" : appkey, #您申请的appKey
"year" : "", #指定年份,格式为YYYY,如:2015
}
params = urlencode(params) if m =="GET":
f = urllib.urlopen("%s?%s" % (url, params)) else:
f = urllib.urlopen(url, params)
content = f.read()
res = json.loads(content) if res:
error_code = res["error_code"] if error_code == 0:
#成功请求
print res["result"] else:
print "%s:%s" % (res["error_code"],res["reason"]) else:
print "request api error"
if __name__ == '__main__': main()
本文来源:https://www.wddqxz.cn/2067aa036294dd88d1d26b99.html