def getRecipeByIngredients():
payload = {
'fillIngredients': False,
'ingredients': ingredients,
'limitLicense': False,
'number': 5,
'ranking': 1
}
api_key = os.environ['api_key']
endpoint = "https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/findByIngredients"
headers={
"X-Mashape-Key": "mashape key",
"X-Mashape-Host": "mashape host"
}
r = requests.get(endpoint, params=payload, headers=headers)
results = r.json()
title = results[0]['title']
print(title)
使用Spoonacular API搜索配料时,我无法访问食谱标题。我已经按照页面上的所有说明进行操作,并确保我正确地获得了标题,但是当我搜索食谱时,除了GET请求之外没有输出任何内容。有什么想法吗?
答案 0 :(得分:0)
看起来您的代码大部分是正确的。
以下是必要的更改:
ingredients
变量赋值api_key
标头的值是X-Mashape-Key
我在下面对您的代码进行了相关更改:
import requests
def getRecipeByIngredients(ingredients):
payload = {
'fillIngredients': False,
'ingredients': ingredients,
'limitLicense': False,
'number': 5,
'ranking': 1
}
api_key = "your-api-key"
endpoint = "https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/findByIngredients"
headers={
"X-Mashape-Key": api_key,
"X-Mashape-Host": "mashape host"
}
r = requests.get(endpoint, params=payload, headers=headers)
results = r.json()
title = results[0]['title']
print(title)
getRecipeByIngredients('apple')
如果您有兴趣,我已经编写了一个Python程序包,该程序包包装了Spoonacular API并使其易于访问其每个端点。该软件包为available on GitHub。