Sub topic 3
robots.txt: what a crawler may fetch
This chapter assumes you know HTTP requests and status codes, can read HTML links, and can follow short Python snippets. It covers the file crawlers read before they fetch your pages, and the mistakes people make with it.
What robots.txt is
robots.txt is a plain text file at the root of a host, for example https://example.com/robots.txt. It tells crawlers which URL paths they may request. It controls crawling, not indexing. A crawler fetches this file first and caches it, then checks each candidate URL against the rules.
Each host and protocol has its own file. https://example.com/robots.txt does not cover https://blog.example.com/.
Reading the rules
User-agent: *
Disallow: /private/
Allow: /private/public-report.html
User-agent: ExampleBot
Disallow: /search
Sitemap: https://example.com/sitemap.xmlUser-agentstarts a group and names the crawler it applies to.*means any crawler without a more specific group.Disallowblocks URLs whose path starts with the given prefix.Allowcarves out an exception inside a blocked prefix.Sitemappoints to a sitemap. It applies to the whole file, not to one group.
A crawler obeys only the one group that best matches its name. It does not combine groups. In this file, ExampleBot follows only its own group. It is blocked from /search but is allowed to fetch /private/, because the * group does not apply to it.
When both Allow and Disallow match a URL, Google uses the most specific rule, meaning the longest matching path. Some parsers use the first matching rule in file order instead.
Testing rules in Python
The standard library can parse the file and answer can_fetch questions.
from urllib.robotparser import RobotFileParser
rp = RobotFileParser()
rp.set_url("https://example.com/robots.txt")
rp.read()
for url in ["https://example.com/", "https://example.com/private/data.html"]:
print(url, rp.can_fetch("ExampleBot", url))Run this against the file above and both URLs print True, for the group-selection reason just described. Change the agent name to "OtherBot" and the second URL becomes False.
Python's parser returns the first matching rule in file order. With the example file, it would report /private/public-report.html as blocked, because Disallow: /private/ appears before the Allow line. Google would allow it. When you depend on Allow exceptions, list the more specific rule first.
How missing files and errors are treated
The status code of the robots.txt response changes the crawler's behavior.
| Response | Typical crawler behavior |
|---|---|
| 200 with rules | Follow the rules |
| 404 or other 4xx | Treat as no restrictions |
| 5xx or timeout | Assume the site is unavailable and pause crawling, or use the last cached copy |
Common errors
- Using Disallow to keep a page out of search. A blocked URL can still be indexed if other pages link to it, usually without a description. Blocking also prevents the crawler from seeing a
noindextag on that page. The next chapters cover this. - Treating robots.txt as security. The file is public and compliance is voluntary. Protect sensitive content with authentication.
- Blocking CSS or JavaScript. Search engines may then be unable to render the page correctly.
- Forgetting that rules are prefix matches.
Disallow: /searchalso blocks/search-tips. - Assuming groups merge. A specific
User-agentgroup replaces the*group for that crawler.
Exercise
A site's robots.txt contains "User-agent: *" with "Disallow: /drafts/", plus a separate group "User-agent: ExampleBot" with "Disallow: /search". May ExampleBot fetch /drafts/post1.html? Explain why.
Show answer
Yes. ExampleBot follows only its own group, which blocks /search. The "*" group, including "Disallow: /drafts/", does not apply to it. To block /drafts/ for ExampleBot, repeat that rule inside its group.