Concepts / HTML Structure and Parsing

HTML Structure and Parsing

Web scraping is a program-based technique for retrieving web pages and extracting specific data from their HTML source code, mimicking what a browser does but automating the data extraction process.

  • Programming

From Browsing to Scraping

When you browse manually, you enter a URL, a browser sends a request to a server, and the server responds with HTML, CSS, and JavaScript. The browser renders that content into a page that you read. Web scraping changes the role of the reader: a program sends the request, retrieves the page's HTML source, and extracts specific information from that underlying data instead of presenting the page for a human to read.

Definition

Web scraping is a program-based technique for retrieving web pages and extracting specific data from their HTML source code. It mimics what a browser does while automating the data-extraction process.

URLURLBrowser requestProgram requestHTML, CSS, JavaScriptHTML sourceReadable pageTarget dataopensreceivesrenderssendsretrievesextracts
What changes when a person reads a rendered page versus when a program retrieves and extracts its HTML?

The Retrieval Pipeline

The fundamental scraping workflow has five connected stages. First, the program sends an HTTP request. Second, it receives HTML. Third, it parses the HTML structure. Fourth, it extracts the target data. Finally, it stores or processes the results. Parsing is the step that turns the received HTML structure into something the program can examine for the particular information it needs.

HTTP requestHTML responseParse structureExtract target dataStore or processreceivesexamineslocatespasses
What happens after a scraper begins acting like a browser?
Worked example

Tracing a Focused Scrape

Suppose a program needs to collect headlines from a news feed. Trace the program's work using the scraping workflow.

  1. 1
    Request

    The program sends an HTTP request for the news-feed page.

  2. 2
    Receive

    The server returns the page's HTML source.

  3. 3
    Parse

    The program examines the HTML structure rather than relying on a human to read the rendered page.

  4. 4
    Extract

    The program selects the headline information it needs from the parsed page.

  5. 5
    Process

    The collected headlines can be stored or processed as results.

Result

The program has automated a focused data-collection task: retrieve a page, inspect its HTML, extract headlines, and handle the results.

Nested HTML and Target Data

HTML is treated here as structured source rather than as a finished visual page. A parser examines that structure so the scraper can move from the page's HTML to the specific data it wants. The important mental model is a path: the program receives a complete page, identifies the relevant part of its nested structure, and extracts the target information instead of treating the whole response as one undifferentiated piece of text.

Web pageHTML sourcePage contentTarget dataHyperlinkscontainsorganizesembedsselected by parser
How does a parser move from a page's nested HTML structure to the specific data selected by a scraper?

What do you think happens?

A scraper has retrieved a page but has not yet examined its HTML structure. What must happen before it can collect a focused dataset?

  • The program must parse the structure and extract the target data

  • The program has already collected the target data

  • The program must wait for a person to read the rendered page

Reveal answer
Answer:

The program must parse the structure and extract the target data.

Retrieval supplies the HTML source, but scraping also requires structural analysis and targeted extraction.

Links as Traversal Paths

A scraper can stop after processing one page, but it can also extract hyperlinks from that page and follow them. Each extracted link can lead to another request. The next page is then retrieved, parsed, and searched for more target data and more links. This turns a single-page extraction task into a sequence of connected page visits.

Page ALink to Page BPage BPage B datacontainsrequestsparse and extract
How does a link found on one page lead to the retrieval and processing of another page?
Worked example

Following Two Connected Pages

Trace a scraper that begins with one page, finds a link, and processes the linked page.

  1. 1
    Start with Page A

    The scraper retrieves Page A and examines its HTML.

  2. 2
    Extract a link

    The scraper identifies a hyperlink embedded in Page A's HTML.

  3. 3
    Request Page B

    The scraper follows the extracted link by retrieving the linked page.

  4. 4
    Process Page B

    The scraper parses Page B, extracts its target data, and can also identify further links.

Result

Link extraction connects page processing into a traversal: one processed page supplies a path to another page.

Scrapers and Search Spiders

Search engines apply the same basic principles at a much larger scale. Automated spiders retrieve pages, extract hyperlinks, follow those links, and repeat the process to discover and index pages across the web. Search engines also measure page importance using patterns such as link frequency: when many pages link to a page, that pattern is interpreted as a signal that the page is valuable.

Known pageRetrieve pageExtract hyperlinksDiscover linked pagesBuild indexrequestinspect HTMLfollowcatalog contentcontinue traversal
How do automated spiders use retrieval and link traversal to discover pages and add their contents to an index?
AspectWeb scrapingSearch-engine spidering
Main purposeExtract specific data from web pagesDiscover pages and build an index
Typical scopeA focused dataset or a limited set of pagesLarge-scale, continuous traversal
Link followingMay stop after extracting data or may follow selected linksSystematically follows links to discover more pages
Example goalCollect product prices, headlines, or directory informationCatalog web content and assess page importance

Spidering is a specialized large-scale application of scraping, while scraping can remain focused on a small dataset.

Common Conceptual Mistakes

  • Common mistake

    Treating scraping as manual browsing

    What it looks like

    Assuming a person must read each rendered page for the data to be collected

    Why it fails

    Scraping automates retrieval and extraction from the HTML source instead of relying on a human to read the rendered page.

    Do this instead

    Think of the program as retrieving the source and analyzing it directly.

  • Common mistake

    Stopping the explanation at the HTTP request

    What it looks like

    Describing a scraper as a tool that only downloads a page

    Why it fails

    The workflow also includes parsing the HTML structure, extracting target data, and storing or processing the results.

    Do this instead

    Trace all five stages: request, receive, parse, extract, and store or process.

  • Common mistake

    Assuming every scraper is a search-engine spider

    What it looks like

    Calling a program that gathers headlines from a limited set of pages a spider

    Why it fails

    Focused scraping and large-scale spidering have different purposes and scopes.

    Do this instead

    Use scraping for focused extraction and spidering for continuous traversal and indexing.

  • Common mistake

    Ignoring links after extracting page data

    What it looks like

    Treating a page as isolated even when its hyperlinks lead to relevant pages

    Why it fails

    Following extracted links is what allows a scraper to traverse connected pages and is central to search-engine crawling.

    Do this instead

    Decide whether the task ends at one page or continues by requesting linked pages.

Practice the Mechanism

MEDIUM

A program retrieves a page, identifies several hyperlinks, follows two of them, and extracts headlines from the linked pages. Explain which parts of this activity are scraping, which part is traversal, and why the overall process resembles search-engine spidering.

Hints
  • Start by naming the retrieval, parsing, and extraction actions.
  • Then identify the action that connects one page to another.
  • Compare the program's purpose and scope with focused scraping and search-engine spidering.
Best practice

When analyzing any scraping task, narrate the state at each stage: the program has sent a request, received HTML, parsed the structure, extracted a target, and either processed the result or used a link to continue. This prevents the common mistake of treating retrieval, parsing, extraction, and traversal as one indistinguishable action.

Key Takeaways

  1. 1
    Web scraping retrieves web pages programmatically and extracts specific data from their HTML source.
  2. 2
    The core workflow is request, receive HTML, parse the structure, extract target data, and store or process the result.
  3. 3
    Scraping differs from manual browsing because the program analyzes source data rather than displaying a rendered page for a person to read.
  4. 4
    Extracted hyperlinks can become paths to additional pages, turning single-page extraction into web traversal.
  5. 5
    Search-engine spidering applies these principles continuously and at large scale to discover pages, build an index, and assess page importance through patterns such as link frequency.

At a glance

  • Web scraping is automated retrieval and targeted extraction from HTML source.
  • Parsing connects the received page structure to the specific data a scraper needs.
  • Following hyperlinks lets a scraper traverse connected pages.
  • Search-engine spiders are large-scale scrapers used for discovery and indexing, not merely focused data collectors.

Related concepts

More concepts starting with H