Scraping Multiple Sites Into One Normalized Schema

Five source sites, five different markup shapes, one Qasida table. sources.py and a parser_type string are what keep the crawler from turning into five unrelated codebases.

Scraping Multiple Sites Into One Normalized Schema

Five sites, one schema. The interesting design decision isn't the scraping code for any single site — it's the seam between "site-specific extraction logic" and "one Qasida record," and how thin that seam is kept.

One SourceWebsite row, one parser_type, one function
One SourceWebsite row, one parser_type, one function

ensure_sources() reconciles the table on every deploy; only is_active survives an operator's pause.

Sources live in code, not only in the database

core/sources.py declares the default crawl targets as a plain Python list:

DEFAULT_SOURCES = [
    {
        'url': 'https://www.mynaatbook.com/',
        'name': 'My Naat Book',
        'parser_type': 'mynaatbook',
        'is_active': True,
    },
    ...
    {
        'url': 'https://www.qasidacollection.com',
        'name': 'Qasida Collection (archive)',
        'parser_type': 'wayback',
        'is_active': True,
    },
]

ensure_sources() runs from the entrypoint on every deploy and reconciles the SourceWebsite table against this list — but only refreshes descriptive fields, deliberately leaving is_active alone:

def ensure_sources(model):
    for spec in DEFAULT_SOURCES:
        obj, was_created = model.objects.get_or_create(url=spec['url'], defaults={...})
        if was_created:
            created.append(spec['name'])
            continue
        changes = {}
        if obj.name != spec['name']:
            changes['name'] = spec['name']
        ...

Keeping the canonical list in code means a fresh deployment starts with the full set of sources instead of an empty table — but an operator's decision to pause a misbehaving site in the admin survives every restart, because that field is explicitly excluded from the reconciliation.

parser_type is the whole indirection

Each SourceWebsite row carries a parser_type string, and run_crawlers() dispatches on it against a flat dict:

scrapers = {
    'mynaatbook': scrape_mynaatbook,
    'desertechoblog': scrape_desertechoblog,
    'damas': scrape_damas,
    'midhah': scrape_midhah,
    'generic': scrape_generic,
    'wayback': scrape_wayback,
}
for website in active_websites:
    scraper = scrapers.get(website.parser_type)
    if scraper is None:
        print(f"Unknown parser type '{website.parser_type}' for {website.name}")
        continue

That's it — no plugin registry, no dynamic import, no abstract base class with five required methods. A new source with genuinely novel markup gets a new function and a new string; a source with no bespoke parser at all falls back to scrape_generic, which tries schema.org JSON-LD first and the densest text block second (core/extract.py).

The four scrapers are honest about disagreeing with each other

What normalizes the output isn't a shared base class — it's that every scraper ends at the same Qasida.objects.create(...) call with the same field names, however differently it got there. scrape_mynaatbook reads a React bundle with regex; scrape_damas walks a WordPress REST API, extracting a PDF's text layer when the post body is empty, falling back to OCR when the PDF's text layer turns out to be shattered; scrape_midhah reads schema.org MusicComposition JSON-LD off a Next.js site whose class names are build-hashed and therefore useless to scrape structurally.

Each scraper also degrades on its own terms. scrape_damas keeps a scan-only post rather than discarding it when no legible text exists — "the scans are the content: keep the post and show the images rather than dropping it" — and only deletes the row if every image candidate turns out unreadable. That per-source judgment call about what counts as "worth keeping" doesn't belong in a shared abstraction; it belongs in the function that actually understands that source's failure modes.

Politeness is the one thing genuinely shared

The one piece of infrastructure every scraper does share is fetching.py's polite_get() — one request at a time per host, a per-host minimum delay (1.5s default, 6s for qasidacollection.com, 4s for web.archive.org), exponential backoff on 429/500/502/503/504, and Retry-After honored when a server sends it. Rate-limiting logic is the part that's genuinely the same problem no matter which site you're hitting, and it's the one abstraction that earned being shared.

Series: Qasida App.