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























