robots.txt first, and what happens when a site says stop
Every URL is checked against the site's robots.txt before it is fetched, with two exceptions we name. When a site puts up a wall, the run stops and says so in plain words.
Two rules cover most of how our scrapers behave toward the sites they read. The site's robots.txt is obeyed for every URL, and a wall is a reason to stop, not a puzzle.
Two of our actors read pages that robots.txt does not open to them: the public Meta Ad Library, and TikTok Shop product pages, whose robots.txt cannot be read without solving a captcha. Each was decided by itself and kept narrow, and each stops at the first wall. The scrapers page names them and their limits.
Check first, fetch second#
The check is a few lines of standard library. It runs before every request, and the answer is cached per site so it does not turn into extra traffic.
from functools import lru_cache
from urllib.parse import urlsplit
from urllib.robotparser import RobotFileParser
UA = "ExampleBot/1.0 (+https://example.com/bot)"
@lru_cache(maxsize=256)
def _rules(origin: str) -> RobotFileParser:
rp = RobotFileParser(origin + "/robots.txt")
rp.read()
return rp
def allowed(url: str) -> bool:
parts = urlsplit(url)
return _rules(f"{parts.scheme}://{parts.netloc}").can_fetch(UA, url)A page that is closed is skipped. Not fetched "just once to see", not fetched at a low rate. Skipped.
The built-in reader used here is short, and it is simple: it takes the first matching rule and does not understand the wildcard characters * and $. For files made of plain prefix rules that is fine. For files with wildcards or an Allow carved out of a broad Disallow it will say yes where the standard says no. robots.txt readers disagree lists where, and the lab lets you try your own file.
A wall stops the run#
Sometimes a site answers with a captcha, a "verify you are human" page or an "access denied" that has nothing to do with the page you asked for. A scraper that treats that as a retry condition ends up hammering a site that has just asked it to go away.
Ours treat it as an answer. The run raises, stops, and puts the reason in the run status.
WALL_MARKERS = ("captcha", "verify you are human", "access denied", "unusual traffic")
class Blocked(Exception):
"""The site asked us to stop. We stop and report it."""
def looks_blocked(html: str) -> bool:
head = html[:20_000].lower()
return any(marker in head for marker in WALL_MARKERS)What we do not do#
- No captcha solving.
- No stealth plugins, and no rotating user agents.
- No logins, and nothing behind a paywall.
- Delays between requests, and a cap per run, so one run never hammers one host.
It is also cheaper. A scraper that fights a wall burns proxy traffic, gets the IP range flagged and still returns nothing. A scraper that stops returns a clear failure the same day, and the buyer can decide what to do with the job.
written
7 min readRemote job APIs: five free feeds, and the catch in each
Remote OK, We Work Remotely, Jobicy, Working Nomads and Arbeitnow publish their jobs without a key. What each feed holds, how far back it goes, the terms, and sixty lines of Python that put them in one list.
5 min readFacebook Ad Library API: which ads you get, which you don't
Meta's API returns political ads from everywhere and every kind of ad shown in the UK or the EU. An ad shown only in the US for running shoes is not in it. What each way into the Ad Library gives you, and how to keep the ads the API leaves out.
6 min readFree exchange rate API with no key: ECB rates in Python
The European Central Bank publishes its daily euro rates as plain files: no key, no sign-up. What is in them, the three things that trip people up (weekends, holidays, retired currencies) and about forty lines of Python that handle all three.