How Data Appears on the Web
Before writing a scraper, it is important to understand where the data actually lives. A website may look like a simple document to a human, but the information displayed in a browser can be produced and delivered in many different ways.
In the simplest case, the data is already present in the HTML document returned by the server. A request is made to a URL, the server responds with HTML, and the browser displays it. The scraper can download that HTML and extract the information it needs.
Plain HTML
Plain HTML is probably the easiest case for a scraper. The requested page contains the data directly in elements such as headings, paragraphs, links, tables, lists, and other HTML elements.
For example, a product page might contain its name, price, description, and other information directly in the returned document. A scraper can request the page, parse the HTML, locate the relevant elements, and extract their contents.
This is the basic model that many people have in mind when they first think about web scraping: download a page and read the data from it.
Server-Side Rendering
Many websites do not consist of manually written HTML pages. Instead, the server generates HTML dynamically using a server-side programming language or template engine.
A template might contain placeholders for a product name, price, image, or list of articles. When the server receives a request, it fills those placeholders with data and generates the final HTML document that is sent to the browser.
From the scraper's perspective, this can still be relatively straightforward. It does not matter whether a human wrote the HTML or a template engine generated it. If the final response contains the data, the scraper can extract it from the response.
Pagination
One page rarely contains all of the available data. Websites commonly divide large collections into multiple pages.
A search result might contain twenty items on the first page, followed by links such as Next, page numbers, or URLs containing a page parameter. A scraper can follow those links and continue collecting data until there are no more pages.
Pagination introduces another problem: the scraper has to understand when the collection is finished. Some websites provide a clear final page, while others continuously generate new page numbers or use less obvious navigation mechanisms.
Infinite Scroll
Modern websites often avoid traditional pagination altogether. Instead, more content appears automatically as the user scrolls down the page.
This is commonly called infinite scrolling. Initially, the page might contain only a small number of items. When the user approaches the bottom, the browser loads another batch of data and adds it to the page. The process can repeat indefinitely.
For a human, this feels like one continuously growing page. For a scraper, however, there may be no obvious next URL to request. The scraper has to determine how additional data is loaded and reproduce the appropriate behavior.
JavaScript-Rendered Content
Another common approach is to send a relatively small HTML document to the browser and let JavaScript build the actual page.
In this situation, the initial HTML response may contain little or none of the information that the user sees. JavaScript executes in the browser, makes additional requests, receives data, and creates or modifies HTML elements on the page.
This creates an important distinction between the HTML source received from the server and the DOM that exists after the browser has executed JavaScript.
A traditional scraper that simply downloads the original HTML may therefore see an almost empty page even though a human visitor sees a large amount of information.
APIs
Sometimes the data is not actually delivered as HTML at all. The JavaScript running on the page may communicate with an API and receive structured data, often in JSON format.
The browser can then use that response to build the interface shown to the user. In such a case, the information displayed on the page may originate from an API rather than from the HTML document.
For a scraper, discovering such an API can be extremely useful. Instead of extracting text from rendered HTML, the scraper can potentially request the structured data directly.
However, APIs introduce their own complications. They may require authentication, session information, specific request headers, tokens, parameters, or other state maintained by the browser. Some APIs may also be private implementation details rather than documented public interfaces.
Multiple Layers at Once
Real websites often combine several of these techniques rather than using just one.
A website might initially render a page on the server, provide pagination for some sections, use JavaScript to load additional content, request more records from an API when the user scrolls, and maintain the user's state through cookies and browser storage.
This means that asking "Where is the data?" is often more useful than simply asking "What HTML should I scrape?"
The Difficulty of the Traditional Approach
Traditional scraping approaches often try to reproduce the process that the website expects from a client. A scraper sends HTTP requests, constructs URLs, manages cookies, parses responses, executes or simulates JavaScript when necessary, discovers API endpoints, handles pagination, and maintains whatever state the website requires.
Each additional layer increases the amount of work required to make the scraper behave correctly.
Even when the data is publicly visible to a normal visitor, accessing it programmatically can require understanding how the entire website works. A scraper may need to reproduce request headers, session state, API calls, JavaScript-generated parameters, pagination logic, or other browser behavior.
This becomes especially difficult when the website changes. A small change to the page structure can break an HTML selector. A new JavaScript application can change how content is loaded. An API can change its parameters. Pagination can be replaced with infinite scrolling. A previously accessible endpoint can start requiring additional browser state.
The result is that the scraper can become a second implementation of the website's client-side behavior. Instead of simply collecting data, the scraper increasingly has to understand and reproduce the machinery that a normal browser already handles automatically.
The Browser Already Solves Many of These Problems
This is one of the reasons the browser is interesting as a scraping environment.
A normal browser already knows how to request HTML, execute JavaScript, maintain cookies, store local data, follow redirects, render pages, communicate with APIs, and update the DOM as a website changes.
Instead of reproducing all of that behavior in a separate scraping application, an alternative approach is to let the browser do what it already does and build the scraper around it.
The rest of this book explores what that approach looks like in practice, including how a Chrome extension can access the page, extract data, communicate with the browser, and maintain scraper state using local storage.