Web Scraping20 min read
Handling Missing Data
Learn how to safely handle missing tags and inconsistent HTML so your scraper never crashes.
David Miller
December 21, 2025
0.0k0
Real websites are messy.
Some items miss fields: - no price - no rating - broken tags
Your scraper must not crash.
Bad approach ```python price = item.select_one(".price").text # may crash ```
Safe approach ```python tag = item.select_one(".price") price = tag.text.strip() if tag else "N/A" ```
Wrap in try-except ```python try: price = item.select_one(".price").text.strip() except AttributeError: price = "N/A" ```
Flow ```mermaid 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