Sub topic 7
Respecting robots.txt
This chapter assumes you can read short Python programs, know basic HTTP status codes, and have seen the URL frontier from the previous chapters. Here you add the check that decides whether a URL may be fetched at all.
What robots.txt is
robots.txt is a plain text file served at /robots.txt on a host's root. It lists which paths a crawler should not fetch. It is a convention, not an access control: nothing stops a crawler from ignoring it, but a well-behaved crawler never does.
User-agent: MyCrawler
Disallow: /private/
Crawl-delay: 5
User-agent: *
Disallow: /searchA crawler follows the group whose User-agent line matches its name. If none matches, it uses the * group. Each Disallow line names a path prefix that is off limits.
Checking rules with the standard library
Python's urllib.robotparser downloads and parses the file for you.
from urllib.robotparser import RobotFileParser
rp = RobotFileParser('https://example.com/robots.txt')
rp.read()
agent = 'MyCrawler'
if rp.can_fetch(agent, 'https://example.com/private/page'):
print('allowed')
else:
print('disallowed')
print(rp.crawl_delay(agent)) # None if no Crawl-delay appliescan_fetch answers whether a given agent may fetch a URL. crawl_delay returns the declared delay, which you can feed into the per-host delay of your frontier.
Where the check fits
Check robots.txt after taking a URL from the frontier and before fetching it. Rules belong to an origin, meaning scheme, host, and port. https://example.com and http://example.com can serve different files, so cache one parsed file per origin.
The crawl loop from the earlier chapter does exactly this with a dictionary keyed by origin:
if origin not in robots:
rp = RobotFileParser(origin + '/robots.txt')
rp.read()
robots[origin] = rp
if not robots[origin].can_fetch(AGENT, url):
continueCommon errors
- Using a different name in the check than in the request. Send the same string in the
User-Agentheader and incan_fetch. Otherwise the site's rules for your crawler do not apply to the requests it receives. - Fetching robots.txt for every URL. This wastes requests and breaks politeness. Cache it per origin, and refresh it after some hours in a long crawl.
- Treating every failure the same. The usual convention is as follows. A 404 means no restrictions. A 401 or 403 means treat the site as off limits. A 5xx or a timeout means the rules are unknown, so retry later and do not crawl in the meantime.
- Ignoring Crawl-delay. It is not part of the original standard and some sites do not set it, but when present it is the site's stated wish. Use the larger of it and your default delay.
- Assuming robots.txt hides content. The file is public, and listing a path advertises it. Do not rely on it for secrecy.
Exercise
A crawler gets a 503 response when it requests /robots.txt for a host. Should it crawl pages on that host right away? Explain.
Show answer
No. A 503 means the rules are unknown, not that there are none. The crawler should delay and retry fetching robots.txt later, and crawl the host only once it has a valid answer or a clear 404 (which means no restrictions).
Exercise
Why should the robots.txt cache be keyed by origin (scheme, host, and port) and not only by host name?
Show answer
Each origin serves its own /robots.txt, and the files can differ. Keying by host alone could apply the rules for http://example.com to https://example.com, or to a different port, where they may not match.