Web scraping, step by step
20 short lessons for someone who can run a script from a terminal and has not scraped a site before. Together they build one script. It starts as a single request. By the end it runs every day, follows the site's rules, keeps a record of each run and stops itself when a run goes wrong. The last module swaps the request for a browser, for pages that build themselves with scripts.
The web scraping guide covers the whole subject on one page, and the library has a page for each question a lesson raises.
- Fetch one page
- Check its status and a word it must hold
- Skip what robots.txt disallows
- Wait between requests
- Follow links on one site
- Stop at a page limit and a depth
- Write one row per page to a dated file
- Add one line to the run log
- Start on a schedule, one run at a time
- Read robots.txt again before each run
- Stop with an error on a bad run
- Open a browser when a page needs one
- Click a link and wait for the new page
- Close every session it opens
Module 6 wraps nothing. It takes the place of module 1 when a page needs it, and every frame around module 1 stays as it is.
Lessons
20 of 20 lessons published. Each lesson takes the script one step further, so read them in order.
Fetch one page
After this module you can fetch one page from a script and tell a good answer from a bad one.
Module 1 · 2 lessons
- 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. 5 min
- 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. 7 min
Follow the site's rules
After this module you can read what a site asks, slow down, and avoid asking twice.
Module 2 · 3 lessons
- 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. 9 min
- 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. 9 min
- 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. 8 min
Crawl one site, within limits
After this module you can go from one page to a bounded set of pages on one site.
Module 3 · 3 lessons
- 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. 9 min
- 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. 6 min
- 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. 7 min
Keep records of every run
After this module you can keep every run as a file with one row per page and a log line.
Module 4 · 3 lessons
- 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. 7 min
- 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. 8 min
- 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. 10 min
Run it unattended
After this module you can leave the job on a schedule, have it stop itself on a bad run, and review it from its own log.
Module 5 · 5 lessons
- 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. 10 min
- 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. 10 min
- 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. 9 min
- 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. 11 min
- 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. 7 min
Read pages that need a browser
After this module you can tell when a page needs a browser, drive one from a program and close every session you open.
Module 6 · 4 lessons
- 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. 7 min
- 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. 4 min
- 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. 5 min
- 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. 5 min