Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
BOOK 000 · CHAPTER 8 · HTML Basics

HTML Basics

On this pageWhat HTML Really IsViewing HTML Behind a Real PageHTML Is a Tree, Not a String

Once your Python program can fetch information from the web, the next challenge is understanding what it has retrieved.

When a website doesn’t offer an API — which is very common — the content arrives as HTML, the structural language of the web.

To a browser, HTML describes how a page should look.

To a human, it’s text sprinkled with angle brackets.

To Python, it’s a rich, hierarchical document that can be navigated and analyzed.

Understanding just a few core ideas will make web pages far less mysterious.

What HTML Really Is

HTML — HyperText Markup Language — is not a programming language. It’s a description of a page: a hierarchy of nested elements that serve different roles.

A small example:

<h1>News Headlines</h1>
<p>Python 3.12 Released!</p>
Listing 432 — What HTML Really Is

To a human reading it in the browser, this becomes bold text at the top followed by a paragraph. To a machine, it becomes a tree of tags: - <h1> — a main heading - <p> — a paragraph

Nearly every component on a webpage — links, images, lists, tables — is represented as a combination of tags.

Learning the most common ones is enough for most scraping tasks: - <h1>,<h2> … for headings - <p> for paragraphs - <a> for links - <ul> and <li> for lists and list items - <table>,<tr>,<td> for tables

Viewing HTML Behind a Real Page

Let’s visit a simple site using Python and see what HTML looks like when downloaded.

import requests

url = "https://example.org"
resp = requests.get(url, timeout=10)
resp.raise_for_status()

print(resp.text[:300], "...")
Listing 433 — Viewing HTML Behind a Real Page

Running this prints the actual raw HTML returned by the server.

It contains everything the browser uses to build the visible page: tags, attributes, nested elements.

You don’t need to memorize any of it.

You only need to recognize that HTML is a structured document Python can parse.

Next, we’ll see how to extract information from this structure.

HTML Is a Tree, Not a String

Although resp.text looks like a long string, conceptually it represents a document tree.

For example, the following snippet

<ul>
  <li>Python 3.12 Released</li>
  <li>AI Research Advances</li>
</ul>
Listing 434 — HTML Is a Tree, Not a String

means:

  • A <ul> (unordered list)
    • contains two <li> (list items)
      • each containing text

This tree structure is what makes scraping possible.

Your program doesn’t “search the text”; it navigates the tree, picking out exactly the elements you want.