Prompt
How do I scrape real estate listings and normalize the fields?
Latest observation
To scrape real estate listings and normalize the fields, think of it as a 3-step pipeline:
- Collect listings
- Extract data from each listing page
- Normalize everything into a consistent schema
1) Define a target schema first
Before scraping, decide the fields you want in every record. For example:
{
"listing_id": "",
"source": "",
"url": "",
"title": "",
"price": null,
"currency": "USD",
"property_type": "",
"status": "",
"beds": null,
"baths": null,
"sqft": null,
"lot_sqft": null,
"address": "",
"city": "",
"state": "",
"zip": "",
"lat": null,
"lng": null,
"description": "",
"images": [],
"agent_name": "",
"agent_phone": "",
"posted_at": null,
"scraped_at": null
}
This gives you a common output even if different sites label things differently.
2) Scrape listing pages ethically and reliably
Best practices
- Check the site’s Terms of Service and robots.txt
- Use rate limiting and polite delays
- Prefer official APIs or data feeds when available
- Avoid login walls or anti-bot bypasses unless you’re authorized
Common tooling
- Requests + BeautifulSoup for static pages
- Scrapy for large crawls
- Playwright or Selenium for JavaScript-rendered pages
- Pandas for cleaning and normalization
3) Extract fields from messy HTML
Real estate sites often use inconsistent labels:
- “Beds”, “Bedrooms”, “BR”
- “Baths”, “Bathrooms”, “BA”
- “Sq Ft”, “Square Feet”, “Interior area”
You’ll want mapping logic.
Example extraction logic
import re
def parse_price(text):
if not text:
return None
text = text.replace(',', '')
m = re.search(r'(\$|€|£)?\s*([\d]+(?:\.\d+)?)', text)
return float(m.group(2)) if m else None
def parse_int(text):
if not text:
return None
m = re.search(r'[\d,]+', text)
return int(m.group(0).replace(',', '')) if m else None
def normalize_beds(text):
if not text:
return None
text = text.lower().strip()
if 'studio' in text:
return 0
m = re.search(r'(\d+(\.\d+)?)', text)
return float(m.group(1)) if m else None
4) Normalize units and formats
Different sources may use:
- Sq ft vs m²
- Price strings with currency symbols
- Dates in different formats
- Different state/country conventions for addresses
Unit normalization examples
- Convert
m²tosqftusing1 m² = 10.7639 sqft - Standardize date fields to ISO 8601 (
YYYY-MM-DD) - Split address into structured components if possible
def sqm_to_sqft(sqm):
return round(sqm * 10.7639) if sqm is not None else None
5) Standardize addresses
Use a geocoder or address parser if you need structured location fields.
Approach
- Keep original
addressstring - Try to parse:
- street
- city
- state/province
- postal code
- Geocode to get
lat/lng
Useful libraries/services:
libpostalfor parsing addresses- Google Geocoding API
- Nominatim / OpenStreetMap
- Here / Mapbox geocoding APIs
6) Handle inconsistent listing pages
Often, the same site has:
- detail pages with all fields
- search pages with summary fields only
- hidden JSON embedded in HTML
Look for structured data
Many listing pages include:
- JSON-LD (
application/ld+json) - OpenGraph metadata
- embedded JavaScript state objects
Example:
import json
from bs4 import BeautifulSoup
def extract_json_ld(html):
soup = BeautifulSoup(html, "html.parser")
scripts = soup.find_all("script", type="application/ld+json")
data = []
for s in scripts:
try:
data.append(json.loads(s.string))
except Exception:
pass
return data
Structured data is often more reliable than scraping visible text.
7) Create a normalization layer
A good pattern is:
Raw extract
Keep exactly what the site gave you:
raw = {
"price_text": "$1,250,000",
"beds_text": "3 beds",
"area_text": "1,842 sqft"
}
Normalized record
Convert to your canonical schema:
normalized = {
"price": 1250000,
"beds": 3,
"sqft": 1842
}
This lets you reprocess later if parsing rules improve.
8) Deduplicate listings
Real estate listings can appear multiple times across:
- search pages
- syndication partners
- mirrored sites
Use:
- source listing ID if available
- canonical URL
- fuzzy matching on address + price + beds + sqft
A common dedupe key:
dedupe_key = f"{address}|{price}|{beds}|{baths}|{sqft}"
9) Suggested architecture
A practical scraper pipeline:
- Crawler discovers listing URLs
- Fetcher downloads page HTML
- Parser extracts raw fields
- Normalizer cleans and standardizes fields
- Storage writes to database/CSV/Parquet
- QA checks missing values and anomalies
10) Example end-to-end sketch
import requests
from bs4 import BeautifulSoup
def scrape_listing(url):
html = requests.get(url, timeout=20, headers={
"User-Agent": "Mozilla/5.0"
}).text
soup = BeautifulSoup(html, "html.parser")
title = soup.select_one("h1")
price = soup.select_one(".price")
record = {
"url": url,
"title": title.get_text(strip=True) if title else None,
"price": parse_price(price.get_text(" ", strip=True) if price else None),
"scraped_at": "2026-10-24"
}
return record
11) Quality checks you should add
- Missing value rate per field
- Outlier detection for price/sqft/beds
- Invalid addresses
- Currency mismatches
- Broken image URLs
- Duplicate listing IDs
12) If you’re scraping at scale
Use:
- Scrapy
- Redis queue
- async requests
- rotating proxies only if permitted
- centralized logging
- retry/backoff handling
- stored raw HTML snapshots for debugging
13) Practical normalization rules
Examples:
beds: convert"3+"to3or3.0baths: keep half baths as2.5sqft: parse commas and remove textprice: remove$,,, and"from"/"starting at"status: map variants toactive,pending,sold,off_marketproperty_type: mapcondo,apartment,townhouse,single_family
If you want, I can give you:
- a Python Scrapy project template,
- a BeautifulSoup parser for a sample listing page, or
- a field-normalization function set for beds/baths/price/address.