Lesson 1 of 9 · 24m
APIs Before Scrapers: Choosing the Real Data Source
Inspect how data reaches the page, compare official APIs with embedded state and rendered HTML, and pick the least brittle source you are allowed to use.
The worst scraping project I inherited was a beautiful browser farm pointed at the wrong problem. Every night it rendered thousands of pages, while the same data was already available in a documented JSON endpoint that cost one HTTP request per record. The first engineering decision in scraping is not which browser driver to use. It is where the data actually enters the system.
Follow the data backward
Open browser developer tools and inspect the network traffic that builds the page. Compare the initial HTML, XHR/fetch calls, embedded state, images, and third-party beacons. You are looking for the first machine-readable representation you are permitted to consume:
official public API best stability and clearest permission
partner/export feed often contractual but designed for volume
JSON embedded in HTML useful but undocumented and versionless
XHR endpoint convenient but may be private or fragile
rendered DOM expensive and brittle, but sometimes necessary
binary/PDF/media extraction problem, not merely crawling problemA site can use React and still send most content in the first HTML response. Another can look simple while every row arrives through a signed POST request. Never infer the data path from the framework name.
Prefer intentional interfaces
If GitHub repository metadata is enough, use the documented API rather than parsing the repository page:
curl -sS https://api.github.com/repos/psf/requests -H 'Accept: application/vnd.github+json' -H 'User-Agent: research-collector/1.0' | jq '{name, stargazers_count, license}'Official APIs usually give you pagination semantics, rate-limit headers, schema stability, authentication, and explicit terms. Those are operational features. A private endpoint may be technically easier but legally and operationally worse.
Embedded state is still a contract problem
Many pages contain JSON in a script tag:
<script id="__NEXT_DATA__" type="application/json">
{"props":{"pageProps":{"product":{"sku":"B-100","price":19.99}}}}
</script>Extracting that JSON is cleaner than reconstructing the DOM, but it is not automatically stable. The shape can change with every deploy. Treat it as an undocumented internal interface: version your parser, capture raw examples, and monitor drift aggressively.
Score the source before committing
Use a short decision matrix:
permission explicit, implied, unclear, prohibited
stability schema versioned, accidental, presentational
cost requests/page, rendering, bandwidth, compute
freshness real-time, crawl-dependent, export latency
identifiers stable IDs or presentation-only text
volume enough pagination depth and filtering for the jobA rendered DOM scraper should be the answer only after the cheaper intentional sources fail this evaluation.
Field notes
- Server-rendered HTML can still contain a complete data model in JSON; inspect before reaching for a browser.
- Infinite scroll usually means a fetch endpoint exists, but private does not mean forbidden or permitted — investigate the terms.
- GraphQL responses mirror the client's query exactly, which makes over-collection easy if you copy browser traffic blindly.
- Never let convenience invert the risk decision. The easiest endpoint is not necessarily the one you should use.
Was this lesson helpful?