This Python script leverages the Spoonacular API to perform a recipe search based on a list of ingredients and retrieve detailed information about the found recipes, including their source URLs. The script is designed to help users find and explore recipes based on specific ingredients.
Key Features of Our Script
Ingredient-Based Search: You simply provide a list of ingredients you have, and the script sends a request to the Spoonacular API to find recipes that include those ingredients.
Detailed Recipe Information: Once the API returns a list of recipes, the script goes a step further. It retrieves detailed information about each recipe, including the recipe title and, most importantly, the source URL. That way, you not only know what to cook but also where to find the full recipe with step-by-step instructions.
Customization: We've made it easy to customize your search. You can adjust the number of results you want to retrieve, giving you control over how many recipes you explore.
Get Cooking!
import requests
# Replace with your Spoonacular API key
api_key = "YOUR.API.KEY"
# Define the Spoonacular API endpoint for complex search
search_endpoint = "API.ENDPOINT.URL"
# Ingredients you want to search for
ingredients = ["ingredient.1", "ingredient.2", "ingredient.3"]
# Prepare the API request for complex search
search_params = {
"includeIngredients": ",".join(ingredients),
"apiKey": api_key,
"number": 10, # You can adjust the number of results you want to retrieve
}
# Send the API request for complex search
search_response = requests.get(search_endpoint, params=search_params)
# Check if the search request was successful
if search_response.status_code == 200:
search_data = search_response.json()
# Extract and print recipes
for recipe in search_data["results"]:
title = recipe.get("title", "No Title Available")
# Retrieve more detailed information for each recipe by its ID
recipe_id = recipe["id"]
recipe_endpoint = f"https://api.spoonacular.com/recipes/{recipe_id}/information"
recipe_params = {"apiKey": api_key}
# Send the API request for recipe information
recipe_response = requests.get(recipe_endpoint, params=recipe_params)
# Check if the request for recipe information was successful
if recipe_response.status_code == 200:
recipe_data = recipe_response.json()
source_url = recipe_data.get("sourceUrl", "No Source URL Available")
print(f"Recipe: {title}")
print(f"URL: {source_url}")
print()
else:
print(f"Error fetching recipe information: {recipe_response.status_code}")
else:
print(f"Error: {search_response.status_code} - {search_response.text}")