Web Scraping20 min read

Handling Missing Data

Learn how to safely handle missing tags and inconsistent HTML so your scraper never crashes.

David Miller
November 24, 2025
2.9k60

Real websites are messy.

Some items miss fields:

  • no price
  • no rating
  • broken tags

Your scraper must not crash.

Bad approach

price = item.select_one(".price").text  # may crash

Safe approach

tag = item.select_one(".price")
price = tag.text.strip() if tag else "N/A"

Wrap in try-except

try:
    price = item.select_one(".price").text.strip()
except AttributeError:
    price = "N/A"

Flow

flowchart TD
  A[Find Tag] --> B{Exists?}
  B -->|Yes| C[Read Text]
  B -->|No| D[Use Default]

Remember

  • Missing data is normal
  • Always code defensively
#Python#Intermediate#Errors