Learn web scraping
Each page here answers one question about fetching and reading web pages with a program. The answer comes first. Under it you get a request to paste, and the sources we read with the day we read them.
For the whole subject on one page, read the web scraping guide.
Before the first page
- Client sends:
GET /robots.txtRead the site's rules before any page - Server answers:
Disallow: /searchNo server enforces this rule, so keeping it is up to you
- Client sends:
The first request
- Client sends:
GET /products/42Send one request and read the answer - Client sends:
User-Agent: examplebot/1.0Say who is calling - Server answers:
503 Service UnavailableRead the status code before the body - Server answers:
Retry-After: 120Wait as long as the server asks
- Client sends:
120 seconds later
- Client sends:
GET /products/42Ask again once the wait is over - Server answers:
200 OKCheck that the body is the page you wanted - Server answers:
ETag: "v7"Keep the tag for the next visit
- Client sends:
The next day
- Client sends:
If-None-Match: "v7"Send the tag back to ask what changed - Server answers:
304 Not ModifiedNothing changed, so there is nothing to download
- Client sends:
Web scraping, step by step
A short course on fetching web pages with care. Each lesson has one request you can paste and run.
20 of 20 lessons published
- 01 Your first request: fetch one page and read the answer A scraper starts with one HTTP request. What goes out, what comes back, and how to read the status code before you trust the page.
- 02 Write the script: a page check that ends in exit code 0, 1 or 2 Move the lesson 1 request into a short Python file that checks the page status and one word, then exits 0, 1 or 2 so a shell can tell what happened.
- 03 Read robots.txt: work out which paths a crawler may fetch Fetch a site's robots.txt, decide by hand which of five paths your crawler may request, then check your verdicts against Python's own parser.
- 04 Pace your requests: add a delay and time what it costs Add a pause between requests to the course script, fetch three practice pages with no pause and with two seconds, and compare the two wall times.
- 05 Work from a saved copy: fetch a page once, check it often Save one fetched page to a file, then point the lesson 2 check at that file so you can edit and rerun it as often as you like with no new request.
- 06 List the links: sort one page's links by where they go Ask for the links on one practice page, resolve each against the page address, and count the ones on the same site, the ones elsewhere and the near repeats.
- 07 Crawl a site: one call with a page limit, and what it returned Turn the one-page request into a single crawl call with a page limit written in the script, then read the dated list of 20 pages it returned.
- 08 Set the depth: crawl at depth 1 and 2 and compare the pages Run the lesson 7 crawl at depth 1 and at depth 2 on a practice site, count the pages each returned, and see why depth counts path segments in the address.
- 09 Save a crawl to CSV: one row per page, with a header Add a CSV writer to the lesson 7 crawl, write url, status and bytes for each page to quotes.csv, and read the first rows of a dated 20-row file.
- 10 Keep every run: name each CSV file by its start time Name each crawl's CSV file after the UTC time the run started, so a second run writes a new file beside the first. Two dated runs, two files, 20 rows each.
- 11 Write a run log: one line per crawl in runs/log.txt Have the crawl script append a line to runs/log.txt after each run: start time, pages asked for, rows written, seconds and the exit code.
- 12 Stop on a bad run: keep the last good file when rows drop Have the crawl script count its rows against the last good run, exit with an error when the count falls by more than half, and leave latest.csv as it was.
- 13 Schedule the job: one crontab line that runs the crawl daily Put the course's crawl script on a daily cron schedule, append everything it prints and its exit code to runs/cron.txt, and read the file the next morning.
- 14 One run at a time: lock the job so a second start exits Hold an exclusive lock on runs/job.lock so a second start of the crawl script sends no request and exits 3 while the first run is still fetching.
- 15 Recheck the rules each run: read robots.txt before every crawl Have the scheduled crawl fetch robots.txt first, save it as that day's file, and skip the crawl with exit 4 when the rules block the start page.
- 16 Read the run log: find the run that differs Read the scheduled job's own log one line per run, mark any run whose rules, page count or outcome differs from the usual, and decide what to change.
- 17 Tell when a page needs a browser: fetch it both ways Fetch one script-built practice page with request http and with request browser, look for a word it must hold, and compare the two dated answers.
- 18 Connect to a browser: open one session from your own program Open one cloud browser session from a short Puppeteer program, load the lesson 17 page, read its status, title and quote count, and close the session.
- 19 Click, wait and read: follow a link without a fixed sleep Read the first author on a practice page, click its Next link, wait for the new page with waitForNavigation instead of a sleep, and read the first author again.
- 20 Close what you open: time limits, a finally block and an honest exit code Give a Puppeteer program two time limits and a finally block, point it at a page with no quotes, and watch the read fail, the session close and the exit code turn 1.
Library, 31 pages in 9 topics
Web pages for AI agents
How an agent fetches a page, what it reads first, and what to do with text it cannot trust.
- What llms.txt is and which docs sites serve one llms.txt is a markdown file, usually at a site's root, that lists pages for AI agents. What it holds, what it is not, and what ten documentation sites returned for it.
- AI crawlers in robots.txt: which bots ten sites name and block AI companies send two kinds of client: a crawler that collects pages for training, and a fetcher that reads one page when a person asks. The robots.txt of ten well-known sites read in one run, with which of seven AI clients each file blocks.
- How an AI agent reads the web: search, read and follow links An AI agent reaches the web through three calls: a search, a fetch of one page, and a list of the links on it. Each one run once against the Python documentation, with the body sent and what came back.
- How to build a web fetch tool for an AI agent A fetch tool an agent can call, in one short Python file: it checks robots.txt, holds a time limit and caps the text it returns. Run once against an address a site allows and one it disallows, with what the tool returned for each.
- Prompt injection from web pages: treat fetched text as data Text an agent fetches from a web page arrives in the same place as its instructions. One practice page fetched as markdown in a dated run, then shown inside a wrapper that marks it as data, and the limits your code should hold.
- What an AI web scraper is, shown on one page fetched two ways An AI web scraper fetches a page like any scraper and returns text a language model can read. One practice page fetched as HTML and as markdown in the same run, with the length of each.
- What an MCP tool call is: three JSON messages sent by hand An MCP tool call is a JSON-RPC message sent over HTTP POST. A short Python script sends initialize, tools/list and one tools/call to a hosted server with no SDK, in a dated run, and prints what each one returned.
What a site asks of a client
The rules a site publishes for programs, and how a well-behaved client follows them.
How to ask
What a request says about the client that sent it, and how often to send one.
- How many requests per second to send when you crawl a site No one rate fits every site. Start at one request per second to one host, then pick a delay from a small crawl we timed at three settings.
- How to identify your web crawler to the sites it visits What a site operator can check about your crawler from one request, what they must take on trust, and a dated echo of the line a server received.
- How to write a scraper user agent that names you What a server sees in your scraper's User-Agent header, how to build one with a name, a version and a contact, and one dated request that shows it arriving.
- Rate limits for web scraping, read from the server's side A rate limit is how many requests a server takes from one client in a time window. How servers count, the headers that state the limit, and one dated reading.
How to read the answer
What the status code and the headers tell a client to do next.
- Conditional requests: If-Modified-Since and If-None-Match A conditional request sends a saved date or ETag back so the server can answer 304 with no body. One page fetched twice on 2026-11-21, with status and bytes.
- ETag and Last-Modified: the two HTTP caching validators, read from ten pages An ETag is a server's label for one version of a page, and Last-Modified is the date that version changed. See which of ten documentation pages sent each one on 2026-11-21.
- HTTP status codes list, and what a client does next The HTTP status codes list in five classes, the codes a crawler meets most, and the next step a well-behaved client takes for each, checked against one dated crawl.
- The Retry-After header: how long to wait, in seconds or a date Retry-After tells a client when to try again, as a count of seconds or as an HTTP date. One dated response, and the wait worked out for both forms.
- What an HTTP header is, and the six a scraper reads every time An HTTP header is a name and value line sent with each request and response. See the headers from one real fetch, and which six to check before you parse a page.
How to find the pages
Where a site lists its own addresses, and how to tell what changed since the last visit.
- Crawl budget: how many pages a crawler reads, and which ones A crawl budget is how many of a site's pages a crawler will fetch. A dated run with a page limit shows which sections of a docs site it returned and which it did not.
- How search engine crawlers work: seed, fetch, extract, queue A crawler starts from a seed URL, fetches the page, pulls out its links and queues the new ones. Here is one small crawl, the first twenty pages it returned.
- How to scrape a paginated site and know when to stop Follow a listing from its first page to its last, and stop on the right signal: no next link, the page limit you set, or a page with no items.
- Incremental crawling: check for changes before you refetch Incremental crawling revisits a site, checks known pages for changes and looks for new ones. Two dated crawls of one docs site, 17 hours apart, show the cost.
- What a canonical URL is and how to read one in a crawl A canonical URL is the address a site names as the preferred copy of a page. See how to find it, compare it with the URL you fetched, and what one crawl showed.
- What a sitemap XML file is and what one real file holds A sitemap XML file lists the pages a site wants crawlers to find. The tags, the index file, and one real sitemap counted for URLs and last modified dates.
Pages that build themselves
How to read a page that loads its content with scripts after the first answer.
- How to scrape an infinite scroll page from its data request Find the request a scrolling page sends for each new batch, call it yourself page by page, and check it against the batch the page loaded before any scroll.
- Static vs dynamic pages: what the plain HTML already holds One practice page fetched as plain HTTP and in a browser in the same run, with the quote markup and the script data counted in each raw body, and how to tell which fetch a page needs.
How to take what you need
Ways to pick one part out of a page and leave the rest.
- How to validate scraped data with checks you can count Five checks run over one 20-page crawl of a practice book shop, how many rows each one passed or flagged, and what a flag tells you about a row.
- Structured data: read the JSON-LD a page already carries, from eight pages Many pages describe themselves in a JSON-LD block that is plain JSON, with no selectors needed. See what eight public pages carried on 2026-11-21, and what to do when a page has none.
- XPath vs CSS selectors, five fields written both ways Five fields from one practice page written as CSS selectors and as XPath, both run on the same day, and the two fields only XPath can find by their label.
What is fair to fetch
The limits no file or status code spells out.
- Web scraping ethics, settled before the first request Web scraping ethics as a checklist you finish before the first request, with each answer set in a crawl we ran on a practice site and read line by line.
- What a CAPTCHA is and what it asks of your scraper A CAPTCHA is a test a site sets to tell people from programs. How to spot one in a response, what a 200 status can hide, and why a polite client stops.
Terms
The words the other pages use, each with a short definition.