liveBook · Manning

Fallback

As explained in Learn AI Data Engineering

A fallback strategy is an approach that specifies an alternative method when the preferred method fails or cannot produce a result.

A fallback strategy selects an alternative method when the preferred method fails or produces no result. In web scraping, it keeps extraction pipelines usable across inconsistent page structures. section 10.7 section 10.6

A practical fallback sequence can:

  • Prefer structured data or a primary selector, then try a secondary selector when the first is absent. section 10.7 section 10.6
  • Guard every extraction step so missing elements produce None instead of an exception. section 10.6
  • Treat access failures, timeouts, JavaScript-heavy rendering, and changing layouts as operational cases requiring alternate handling. section 10.7

Fallback price extraction

After locating a product price block, first select the sale-price span; if it is absent, select the regular-price span. If neither exists, retain price as None. section 10.6

Listing 10.4 Finding the title and price in the GORUCK HTML
from bs4 import BeautifulSoup
 
soup = BeautifulSoup(html_content, "html.parser")
 
# Try to find the product title
title_el = soup.find("h1")  #A
title = title_el.get_text(strip=True) if title_el else None
 
# Find the price block used on this product page
price_block = soup.select_one("div.product-block__price")  #B
 
price = None
if price_block:
    # Prefer sale price if present, otherwise fall back to regular price
    price_el = (  #C
        price_block.select_one("span.price-item--sale")
        or price_block.select_one("span.price-item--regular")
    )
    if price_el:
        price = price_el.get_text(" ", strip=True)
 
print(f"Title: {title}")
print(f"Price: {price}")

Inspect a complete BeautifulSoup extraction pattern that combines a required title lookup with sale-price and regular-price fallbacks. section 10.6

How Learn AI Data Engineering covers it

How deep it goes
Demonstrates selector-level fallback and missing-element guards, but stops short of a general framework for maintaining many domain-specific rules.
Examples
Uses product-page title extraction, sale-versus-regular price selection, and multi-site scraping with requests and BeautifulSoup.
What it assumes
Assumes basic Python, requests, BeautifulSoup, HTML inspection, and CSS-selector knowledge while demonstrating the fallback pattern directly.
What you can do after
Build resilient field extraction that returns available values, preserves missing fields as None, and accommodates common page differences.
How it connects
Connects fallback strategies with web scraping, HTML parsing, structured data, CSS selectors, and data pipelines.

Also explained in

Compare how 24 books explain fallback →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI Data Engineering. The section links point to the parts of the book it draws on.