Overview

11 AI-powered web scraping

Building on the earlier hand-coded scraping pipeline, this chapter shifts the focus from brittle, site-specific selectors to places where AI can replace or augment manual work. The manual approach proved workable but hard to scale because every new site, variant page, or HTML change demanded more selectors, more fallback logic, and more maintenance. The core idea is to treat a data pipeline as a set of stages and ask where unstructured content, variability, or judgment calls make AI genuinely useful, while leaving simple deterministic tasks to ordinary code.

The chapter organizes AI use into three kinds of generated data: extracted data that already exists but is hard to retrieve, enriched data that requires inference or categorization, and synthetic data that is generated from scratch for testing or development. For the RuckZone pipeline, the highest-value AI opportunities are URL discovery, HTML cleaning, and product extraction. URL discovery becomes a two-step process in which a search API gathers candidate pages and an LLM ranks them using criteria such as official source, product-page quality, and likelihood of containing useful fields. HTML cleaning is expanded beyond basic tag removal by stripping common noise like headers, banners, reviews, and related-product modules, reducing token usage and improving downstream extraction quality.

The extraction workflow itself replaces manual CSS selectors with a schema-driven prompt and structured output. A Pydantic model defines the fields the model should return, and the prompt tells it to extract only what is explicitly present, prefer null over guessing, and preserve values like price and weight as strings for later normalization. Compared with the manual approach, the AI version extracts more complete product records across many sites with one reusable function, though it still needs validation, confidence review, and cost awareness. The chapter closes by emphasizing batch processing, failure handling, token budgeting, and a practical checklist for deciding when AI is appropriate, along with guidance on when deterministic methods remain the better choice.

Example output after running listing 11.1. Notice that four candidate URLs were found with titles, URLs and search positions.
Example output after running listing 11.2. Notice that the reasoning is based on our prompt and what we deem important to accomplish the task at hand. This is how you can fine tune results and configuration based on your specific use case using natural language instead of code.
A high level example of the type of content listing 11.3 aims to remove. HTML and CSS tags help a website understand how to render, style and process it’s content. While this is valuable for the operation of a website, these “tags” are white noise when trying to extract information from a website.
A comparison of the manual extraction approach versus the AI first approach, clearly showing the value AI brings when extracting valuable data from a website. You should see similar results after running listing 11.6.
The results of running listing 11.7 and the subsequent pipeline outlined in this chapter. Please note that results will vary, but now you have the blueprint for making your extraction pipelines more deterministic when using AI.
Flowchart: "Should I use AI for this step?" Decision tree walking through the key questions from the checklist.

Lab Answers

1.

2.

3.

4.

FAQ

What is the main problem with manual web scraping approaches?Manual scraping works for a few pages, but it does not scale. It often requires site-specific CSS selectors, fallback logic, and lots of maintenance when different sites or page layouts change.
What are the six stages of the enrichment pipeline in Chapter 11?The six stages are: 1) Data Preparation, 2) URL Discovery, 3) HTML Fetching, 4) Image Extraction, 5) AI Extraction, and 6) Target Mapping.
Which stages of the pipeline benefit most from AI?The highest AI payoff is in stages 2, 3, and 5: URL Discovery, HTML Fetching and Cleaning, and AI Extraction. These are the stages where manual approaches tend to break down at scale.
What are the three categories of AI-generated data discussed in the chapter?The three categories are extracted data, enriched data, and synthetic data. Extracted data already exists in the source, enriched data is inferred or added, and synthetic data is generated from scratch.
What is AI extraction?AI extraction means sending raw or cleaned content to an LLM and asking it to pull out specific fields. Instead of writing selectors, you define a schema and maintain a prompt.
How does AI-assisted URL discovery work?It uses two steps: a search API to find candidate URLs, then an LLM to rank those candidates and choose the best product page for extraction.
Why is aggressive HTML cleaning important before sending content to an LLM?Aggressive cleaning removes non-product noise such as navigation, reviews, banners, and headers. This reduces token usage, lowers cost, speeds up responses, and helps the model focus on relevant product content.
Why does the chapter use a Pydantic schema for product extraction?The Pydantic schema defines exactly which fields the model should return and how they should be typed. It acts as both a validation contract for code and an implicit prompt for the model.
What are the main advantages of AI extraction over manual CSS selectors?AI extraction can work across many sites with one prompt, extract more fields without custom site logic, and handle inconsistent HTML better than handcrafted selectors.
What should you consider when using AI in a data pipeline?You should check whether the input is unstructured, whether the logic varies by source, whether a human could do the task with enough time, whether the cost is justified, whether the output can be validated, and what happens when AI makes mistakes.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn AI Data Engineering ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn AI Data Engineering ebook for free