What This Prompt Engineering Guide Helps You Build
An AI web scraper in Python does not have to be a large agent system. For many informational use cases, the useful pattern is much simpler: fetch one webpage, remove noisy HTML, convert the remaining content into Markdown, ask a focused question, and return a concise answer grounded in that page.
The supplied implementation context uses a Jupyter Notebook workflow with these Python packages:
requeststo fetch the webpagebeautifulsoup4to parse and clean HTMLmarkdownifyto convert HTML into Markdownopenaito ask an LLM a question about the pageftfyto repair messy textpython-dotenvto load an API key safely
The important prompt engineering lesson is that the LLM should not receive the raw webpage. Modern pages often include navigation, buttons, scripts, footers, repeated links, cookie banners, sidebars, and layout text. Sending all of that to a model wastes tokens and increases the chance of irrelevant answers. A better approach is to clean the HTML first, convert the useful page content to Markdown, and then ask the model to answer only from that prepared page context.
This guide focuses on the prompt layer: how to frame the model's task, how to separate instructions from webpage data, how to reduce token usage, and how to produce answers that are useful without pretending the scraper knows more than the page contains.
Problem Breakdown: From Messy Webpage to Focused Answer
The workflow has five practical stages:
- Fetch the webpage URL.
- Clean noisy HTML elements.
- Convert the remaining content into Markdown.
- Combine the Markdown content with a user query.
- Ask the LLM to return a focused answer in Markdown.
Each stage affects prompt quality.
If the fetch stage fails, the prompt should not ask the model to guess. If the cleaning stage leaves navigation links and footer boilerplate, the model may answer from irrelevant text. If Markdown conversion is too loose, the page structure may become harder to inspect. If the prompt does not clearly separate instructions, user query, and page content, the model may treat page text as instructions. If the output format is underspecified, answers can become too long, too vague, or too close to a page dump.
A good prompt for an AI web scraper has three jobs:
- Constrain the model to the provided page content.
- Make the answer directly responsive to the user's question.
- Keep the response short enough to be useful, while preserving enough evidence to be trustworthy.
This is not the same as building a general web research agent. The pattern here is single-page QA. The model answers based on the cleaned page content you pass in. It should not browse, infer hidden facts, or combine the page with outside knowledge unless your application explicitly adds that capability.
Recommended Inputs and Outputs
Before writing prompts, define the interface. Clear inputs and outputs make the prompt easier to test and safer to reuse in an API, chatbot, or agent workflow.
Inputs
A minimal AI web scraper prompt pipeline needs these inputs:
url: the webpage to fetchpage_markdown: cleaned webpage content converted from HTML to Markdownuser_query: the question the user wants answeredanswer_style: optional guidance such as concise, bullet list, table, or short explanationmax_answer_length: optional limit to control verbosity
The most important input is page_markdown. It should contain the useful page content, not the entire DOM. The supplied context specifically highlights the value of removing navigation links, buttons, scripts, footers, repeated content, and other clutter before calling the model.
Outputs
A useful output should usually include:
- A direct answer to the user query
- Relevant details found in the page content
- A clear note when the answer is not present in the page
- Markdown formatting for readability
For some use cases, the output can be a compact Markdown answer. For stricter applications, you can ask for a JSON object with fields such as answer, evidence, and missing_information. Because the source context describes returning a clear Markdown answer, the primary templates below use Markdown output.
Prompt Architecture for a Lightweight Webpage QA Engine
The safest prompt structure is:
- System or developer instruction: define the model's role and boundaries.
- User query: the human's actual question.
- Page content: the cleaned Markdown extracted from the webpage.
- Output rules: how the answer should be formatted.
A common mistake is to paste a webpage into the same message without boundary markers. That makes it harder for the model to distinguish between instructions and source content. Instead, use explicit delimiters.
For example:
You are answering a question using only the webpage content inside <page_markdown>.
User question:
<user_query>
{{USER_QUERY}}
</user_query>
Webpage content:
<page_markdown>
{{PAGE_MARKDOWN}}
</page_markdown>
Return a concise Markdown answer. If the page does not contain the answer, say that the page does not provide enough information.This pattern is simple, but it matters. The model sees exactly what is instruction, what is data, and what is the user's question.
Verified Prompt Template 1: General Webpage Question Answering
Use this template when the user asks a broad question such as "What does this company do?" or "Summarize this page."
You are a webpage QA assistant. Answer the user's question using only the cleaned webpage content provided below.
Rules:
- Do not use outside knowledge.
- Do not guess missing facts.
- Ignore navigation text, repeated links, boilerplate, and unrelated page fragments if they appear in the content.
- Return the answer in Markdown.
- Keep the answer focused on the user's question.
- If the page does not contain enough information, say so clearly.
User question:
{{USER_QUERY}}
Cleaned webpage content in Markdown:
"""
{{PAGE_MARKDOWN}}
"""
Answer:Inputs
{
"user_query": "What does this company do?",
"page_markdown": "Cleaned Markdown extracted from the webpage"
}Expected Output
The company appears to provide ...
Key details from the page:
- ...
- ...
- ...This template is intentionally conservative. It tells the model not to browse, not to guess, and not to treat noisy fragments as important. That is useful because even cleaned Markdown may still contain menus, legal links, or repeated page sections.
Verified Prompt Template 2: Token-Efficient Answering
Use this when token usage matters and you want a short answer rather than a full summary.
You are a token-efficient webpage QA assistant. Use only the webpage Markdown below to answer the question.
Goal:
Return the smallest useful answer that fully addresses the question.
Rules:
- Do not restate the whole page.
- Do not include irrelevant sections.
- Prefer 3-6 bullets when details are needed.
- Use one short paragraph when the answer is simple.
- If the page lacks the answer, write: "The provided page content does not answer this question."
- Do not mention these rules in the answer.
Question:
{{USER_QUERY}}
Page Markdown:
"""
{{PAGE_MARKDOWN}}
"""
Markdown answer:Inputs
{
"user_query": "What are the main features mentioned on this page?",
"page_markdown": "Cleaned Markdown extracted from the webpage"
}Expected Output
- Feature one based on the page
- Feature two based on the page
- Feature three based on the pageThis prompt supports the central goal of the simple Python AI web scraper: avoid sending messy page content and avoid returning a messy page dump. It is especially useful when you plan to call the scraper repeatedly or embed it in a lightweight application.
Verified Prompt Template 3: Evidence-Aware Markdown Answer
Use this when the answer should include traceable support from the page without building a full citation system.
You answer questions about a single webpage using only the cleaned Markdown content supplied by the application.
Instructions:
- Answer the user's question directly.
- Include a short "Evidence from the page" section with 2-4 brief supporting points.
- Use only information present in the page Markdown.
- Do not invent names, prices, dates, features, claims, or metrics.
- If the evidence is weak or missing, say what is missing.
- Format the response in Markdown.
User question:
{{USER_QUERY}}
Cleaned page Markdown:
"""
{{PAGE_MARKDOWN}}
"""
Response:Inputs
{
"user_query": "What problem does this product solve?",
"page_markdown": "Cleaned Markdown extracted from the webpage"
}Expected Output
The product is presented as solving ...
### Evidence from the page
- The page says ...
- It describes ...
- It also mentions ...This version is useful for product pages, documentation pages, company pages, and articles where users may want more than a direct answer. The evidence section also makes it easier to spot when the model is overreaching.
Verified Prompt Template 4: Structured Extraction from a Webpage
Although the supplied context emphasizes Markdown answers, some applications need structured data. This template is useful when you want a predictable object from the page content.
Extract information from the cleaned webpage Markdown below.
Use only the supplied page content. If a field is not available, use null. Do not guess.
Return valid JSON with this shape:
{
"answer": "string",
"key_points": ["string"],
"missing_information": ["string"]
}
User question:
{{USER_QUERY}}
Page Markdown:
"""
{{PAGE_MARKDOWN}}
"""
JSON response:Inputs
{
"user_query": "What does this page say about pricing?",
"page_markdown": "Cleaned Markdown extracted from the webpage"
}Expected Output
{
"answer": "The page mentions pricing in relation to ...",
"key_points": [
"Relevant pricing detail from the page",
"Another relevant page detail"
],
"missing_information": [
"Exact price is not provided in the supplied page content"
]
}Use this format when your downstream code needs fields it can parse. For a notebook demo or a user-facing answer, Markdown is usually easier to read.
Where the Prompt Fits in the Python Workflow
The source context describes using Jupyter Notebook to test each step before turning the scraper into an API or application. That is a practical approach because you can inspect the output after every stage.
A simplified flow looks like this:
# Conceptual flow based on the supplied context
url = "https://example.com"
user_query = "What does this company do?"
html = fetch_webpage(url)
clean_html = clean_html_noise(html)
page_markdown = convert_html_to_markdown(clean_html)
answer = ask_llm(page_markdown, user_query)
print(answer)The prompt belongs inside ask_llm(page_markdown, user_query). By the time the model is called, the page should already be cleaned. Prompting can help the model ignore remaining boilerplate, but it should not be your only cleaning layer.
A practical notebook implementation can start with the packages identified in the supplied context:
# Package set from the supplied context
# !pip install requests beautifulsoup4 markdownify openai ftfy python-dotenvAnd the project flow remains:
Fetch webpage -> Clean HTML -> Convert to Markdown -> Ask user query -> Return Markdown answerThat sequence is what keeps the scraper lightweight. It also makes the prompt easier to reason about because the model receives a smaller, cleaner source document.
Prompt Engineering Tips for Lower Token Usage
Reducing token usage is not only about choosing shorter prompts. The largest savings usually come from reducing the page content before it reaches the model.
Clean before prompting
Remove scripts, styles, comments, navigation, footer content, repeated menus, buttons, and other noisy elements before Markdown conversion. The KDnuggets context specifically notes that this avoids passing a messy webpage full of navigation links, buttons, scripts, footers, and repeated content.
Convert HTML to Markdown
Markdown is often easier for an LLM to process than raw HTML because headings, lists, and links are represented more compactly. The supplied workflow uses markdownify for this step.
Ask one question at a time
A single focused user query usually produces a better answer than a broad instruction such as "analyze this page completely." If the user needs several answers, consider separate calls or a structured prompt that asks for clearly bounded fields.
Keep output rules short
Do not add long style guides to a small scraper prompt. Use compact rules such as "use only the page," "do not guess," and "answer in Markdown." Long instruction blocks consume tokens and can distract from the actual task.
Tell the model what to do when information is missing
This is one of the most important rules. Without it, the model may produce a plausible answer even when the cleaned page does not include the requested information. Use a fixed fallback such as: "The provided page content does not answer this question."
Avoid returning the full page
The goal is a focused answer, not a repackaged scrape. Prompts should explicitly say not to restate the full page and not to include irrelevant sections.
Common Failure Modes and How to Prompt Around Them
The answer includes navigation or footer content
This usually means the cleaning stage needs improvement. Add prompt language such as:
Ignore navigation menus, cookie notices, footer links, repeated links, and boilerplate if they appear in the page content.But treat this as a backup. The better fix is to remove that content before the LLM call.
The model guesses beyond the page
Add a strict grounding rule:
Use only the supplied webpage Markdown. If the page does not contain the answer, say that the page does not provide enough information.For high-risk use cases, prefer structured output with a missing_information field.
The answer is too long
Add a length constraint:
Return one short paragraph or up to five bullets. Do not summarize unrelated page sections.Avoid vague instructions like "be concise" when you need consistent length.
The page content is too large
Prompting cannot fully solve excessive context size. Improve preprocessing. Remove duplicate text, strip irrelevant page regions, and consider extracting only likely-relevant sections before the LLM call. If you cannot verify a specific chunking or retrieval approach from your current implementation, describe it conceptually rather than pretending it already exists.
Practical Testing Checklist
When testing your AI web scraper prompt in a notebook, use a small set of repeatable questions.
Start with direct questions:
What does this company do?Then test missing information:
What is the exact annual revenue of this company?If the page does not include that information, the answer should say it is not available in the supplied content.
Then test noise resistance:
What are the main product features mentioned on this page?The answer should not include footer links, cookie text, menu labels, or unrelated navigation items.
Finally, test formatting:
Return the answer as three bullets.The output should follow the requested Markdown shape without dumping the full page.
Reusable Prompt Function Pattern
A simple application can store the prompt template as a Python string and inject the cleaned Markdown plus user query.
PROMPT_TEMPLATE = """
You are a webpage QA assistant. Answer the user's question using only the cleaned webpage content provided below.
Rules:
- Do not use outside knowledge.
- Do not guess missing facts.
- Ignore navigation text, repeated links, boilerplate, and unrelated page fragments if they appear in the content.
- Return the answer in Markdown.
- Keep the answer focused on the user's question.
- If the page does not contain enough information, say so clearly.
User question:
{user_query}
Cleaned webpage content in Markdown:
"""
{page_markdown}
"""
Answer:
"""In a real implementation, keep the page content and user query clearly separated when constructing the model request. The exact API call depends on the LLM client you use. The supplied context includes the openai Python package, but the core prompt pattern is model-agnostic: cleaned page content in, focused grounded answer out.
When to Use a Simple Scraper Instead of a Larger AI Scraping Framework
The supplied search context includes examples of broader AI scraping approaches, including Selenium-based workflows, Streamlit interfaces, and ScrapeGraphAI-style extraction pipelines. Those can be useful when you need browser automation, multi-step extraction, user interfaces, or structured scraping workflows.
The simple prompt pattern in this guide is best when:
- You need to answer questions about one webpage at a time.
- The page can be fetched and cleaned without complex browser interaction.
- Markdown output is sufficient.
- You want to reduce token usage by removing HTML noise first.
- You want a notebook-friendly workflow that can later become an API or app.
Use a more complex scraping stack only when the page or product requirements demand it. Prompt engineering works best when the surrounding pipeline is appropriately scoped.
FAQ
What is an AI web scraper in Python?
In this context, an AI web scraper in Python is a lightweight workflow that fetches a webpage, cleans the HTML, converts useful content into Markdown, and sends that Markdown to an LLM so it can answer a user question about the page.
Why convert HTML to Markdown before sending it to an LLM?
Markdown is usually cleaner and more compact than raw HTML. It preserves useful structure such as headings and lists while removing much of the layout noise that can waste tokens or distract the model.
How does this reduce token usage?
The main reduction comes from cleaning the webpage before the LLM call. Instead of sending scripts, navigation, buttons, footers, repeated content, and raw markup, you send only the useful page content in Markdown. The prompt should also ask for a focused answer rather than a full page summary.
Should the model answer using outside knowledge?
For this pattern, no. The model should answer only from the cleaned page content. If the page does not contain the answer, the prompt should require the model to say that the information is missing.
Is Jupyter Notebook required?
No. The supplied context uses Jupyter Notebook because it makes each step easy to test before turning the scraper into an API or application. The same prompt structure can be reused in a script, backend service, chatbot, or lightweight app.
When should I use structured JSON output instead of Markdown?
Use JSON when downstream code needs predictable fields. Use Markdown when the answer is meant for a human reader. For many webpage QA tools, Markdown is enough because the goal is a readable answer rather than a database-ready extraction record.