An API is a defined set of rules that lets one piece of software communicate with another, allowing applications to request and exchange data in a structured format, such as JSON, without needing to parse raw HTML. When a site loads its data from an API, Scrapy's documentation calls reproducing those requests the preferred approach, since it gives structured, complete data with minimum parsing time.
On the web, "API" usually means a web API: a set of URLs, called endpoints, that accept HTTP requests and return data instead of pages.
How an API Request Works
- The client calls an endpoint: it sends an HTTP request, such as
GET, to a specific URL. - It follows the API's schema: the request must use the parameters and format the API expects, or the server can't return a usable answer.
- It proves who it is: many APIs require an API key or an access token from OAuth.
- The server responds: it returns data, often JSON, plus a status code such as
200for success or429when too many requests were sent.
Common Types of APIs
- REST: an architectural style for web services. Cloudflare notes that most APIs today are REST APIs.
- SOAP: a messaging protocol, rather than a style like REST.
- GraphQL: a query language for APIs. Its official site says clients can request exactly the fields they need and get related data in a single request.
- Webhooks: the server calls your URL when an event happens, instead of you asking. See webhook.
API vs. Web Scraping
| Official API | Web scraping | |
|---|---|---|
| What you get | Structured data, often JSON | HTML built for people |
| Limits | Documented, such as GitHub's 60 unauthenticated or 5,000 authenticated requests per hour | Not published, though sites can still rate-limit |
| Format | Set by the API's schema | Whatever the page's HTML happens to be |
| Coverage | Only the data the owner chooses to expose | Anything shown on public pages |
Some proxy and data-extraction providers offer their own "scraper APIs" that combine proxy infrastructure with built-in parsing, effectively turning an unstructured website into a structured API response on the customer's behalf.
Many Sites Have a Hidden API
Pages that load content with JavaScript often fetch it from the site's own API. Scrapy's documentation recommends finding that request in your browser's network tool and reproducing it. Sometimes the same HTTP method and URL are enough. Other times you also need its headers, body or form parameters. If the response is JSON, you can load it directly instead of parsing HTML.
Retry Only Requests That Are Safe to Repeat
HTTP methods differ in what happens if a request is sent twice. MDN defines a method as idempotent when several identical requests have the same intended effect as one:
- Safe to repeat:
GET,HEAD,OPTIONS,PUTandDELETEare idempotent. - Not guaranteed:
POSTandPATCHare not, so retrying a failedPOSTcan create a duplicate order, message or record. - A fix in progress: an IETF draft defines an
Idempotency-Keyheader that lets servers recognize retries of the samePOST. It lists Stripe, Adyen and others as already using a similar header.
What It Means for Web Scraping
- Check for an API first: an official or hidden API usually gives cleaner data than HTML.
- Respect the documented limits: GitHub's higher limit applies to an authenticated user, so it follows the account, not the IP.
- Retry reads, not writes: automatic retries are safest on
GETrequests.