Web Scraping18 min read
Handling Missing Data
Learn how to safely handle missing tags and inconsistent HTML so your scraper never crashes.
David Miller
Nov 22, 2025
37.6k1,013
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