基于Python的周公解梦api调用代码实例 代码描述:基于Python的周公解梦api调用代码实例 代码平台:聚合数据 #!/usr/bin/python # -*- coding: utf-8 -*- import json, urllib from urllib import urlencode #---------------------------------- # 周公解梦调用示例代码 - 聚合数据 # 在线接口文档:http://www.juhe.cn/docs/64 #---------------------------------- def main(): #配置您申请的APPKey appkey = "*********************" #1.类型 request1(appkey,"GET") #2.解梦查询 request2(appkey,"GET") #3.根据ID查询解梦信息 request3(appkey,"GET") #类型 def request1(appkey, m="GET"): url = "http://v.juhe.cn/dream/category" params = { "key" : appkey, #应用APPKEY(应用详细页查询) "fid" : "", #所属分类,默认全部,0:一级分类 } 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://v.juhe.cn/dream/query" params = { "key" : appkey, #应用APPKEY(应用详细页查询) "q" : "", #梦境关键字,如:黄金 需要utf8 urlencode "cid" : "", #指定分类,默认全部 "full" : "", #是否显示详细信息,1:是 0:否,默认0 } 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" #根据ID查询解梦信息 def request3(appkey, m="GET"): url = "http://v.juhe.cn/dream/queryid" params = { "key" : appkey, #应用APPKEY(应用详细页查询) "id" : "", #解梦ID } 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.wddqw.com/doc/676a96db5f0e7cd18525364d.html