Extracting Recipes from HTML
Introduction: Turning Web Pages into Usable Recipes
Welcome back! In the previous lesson, you learned how to use AI to generate new recipes from a list of ingredients. Now, let’s take the next step: extracting recipes from real-world web pages.
Many cooking websites have great recipes, but the information is often buried in messy HTML code. Our goal is to build a script that can take a raw HTML file, use AI to extract a clean recipe, and then store that recipe in our database. This process is a key part of making our AI Cooking Helper smarter and more useful.
By the end of this lesson, you’ll understand how to automate recipe extraction from HTML using prompt templates, LLM calls, and your existing database setup.
Quick Recall: Recipe Generation with AI
Before we dive in, let’s briefly remind ourselves how we previously generated recipes with AI.
In the last lesson, you learned how to:
- Use prompt templates to ask the AI for a recipe based on a list of ingredients.
- Send these prompts to the AI and receive a structured recipe in response.
- Parse the AI’s response and use it in your application.
This time, instead of generating a recipe from scratch, we’ll use the AI to extract a recipe from a messy HTML page. The process is similar, but the input and prompts are a bit different.
How the Extraction Script Works
Let’s look at the big picture before we break things down.
The script you’ll be working with is called extract_and_store_recipe.py. Its job is to:
- Read a raw
HTMLfile from your computer. - Use AI to extract a clean recipe from that
HTML. - Parse the AI’s response into structured data (
name,ingredients,steps). - Store the recipe in your database, making sure not to add duplicates.
Here’s a simple diagram of the flow:
This script brings together everything you’ve learned so far: prompt templates, LLM calls, and database operations.
Using Prompts to Extract Recipes from HTML
The first step is to get the AI to read the HTML and return a clean recipe. We do this by sending it a carefully crafted prompt.
