基于Python的菜谱大全api调用代码实例
代码描述:基于Python的菜谱大全api调用代码实例
delete键在哪里接口平台:聚合数据
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json, urllib
from urllib import urlencode
#----------------------------------
# 菜谱大全调用示例代码 - 聚合数据
# 在线接口文档:www.juhe/docs/46
#----------------------------------
def main():
#配置您申请的APPKey
appkey = "*********************"
#1.菜谱大全
request1(appkey,"GET")
#2.分类标签列表
request2(appkey,"GET")
#3.按标签检索菜谱
request3(appkey,"GET")
#4.按菜谱ID查看详细
request4(appkey,"GET")
#菜谱大全
def request1(appkey, m="GET"):
url = "apis.juhe/cook/query.php"
params = {
"menu" : "", #需要查询的菜谱名
"key" : appkey, #应用APPKEY(应用详细页查询)
"dtype" : "", #返回数据的格式,xml或json,默认json
"pn" : "", #数据返回起始下标
"rn" : "", #数据返回条数,最大30
"albums" : "", #albums字段类型,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)
ifgroupme的中文意思 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"
#分类标签列表
defc语言培训班地址 request2(appkey, m="GET"):
url = "apis.juhe/cook/category"
documentary是什么意思啊 params = {
"parentid" : "", #分类ID,默认全部
"key" : appkey, #应用APPKEY(应用详细页查询)
"dtype" : "", #返回数据的格式,xml或json,默认json
}
params = urlencode(params)
if m =="GET":
setvalidatorf = 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 python基础代码大全下载"request api error"
#按标签检索菜谱
def request3(appkey, m="GET"):
url = "apis.juhe/cook/index"
params = {
"cid" : "", #标签ID
"key" : appkey, #应用APPKEY(应用详细页查询)
"dtype" : "", #返回数据的格式,xml或json,默认json
"pn" : "", #数据返回起始下标,默认0
"rn" : "", #数据返回条数,最大30,默认10
"format" : "", #steps字段屏蔽,默认显示,format=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"
#按菜谱ID查看详细
def request4(appkey, m="GET"):
url = "apis.juhe/cook/queryid"
params = {
"id" : "", #菜谱的ID
"key" : appkey, #应用APPKEY(应用详细页查询)
"dtype" : "", #返回数据的格式,xml或json,默认json
}
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()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论