Parsing robots.txt with Python urllib.robotparser

The urllib.robotparser module in Python provides a standard framework for reading, parsing, and evaluating robots.txt files to ensure web crawlers respect website scraping policies. By using its core class, RobotFileParser, developers can automatically fetch a site's directives, interpret access restrictions for specific user agents, and determine whether a target URL is safe to crawl before sending request payloads.

How RobotFileParser Works

The parsing workflow within urllib.robotparser consists of three primary phases: fetching the file, parsing its contents into structured rules, and evaluating access permissions.

1. Fetching and Reading the File

To begin parsing, the RobotFileParser instance must be associated with the location of a target robots.txt file. This is handled using the set_url() method. When the read() method is executed, the module uses urllib.request to issue an HTTP GET request to retrieve the document:

import urllib.robotparser

rp = urllib.robotparser.RobotFileParser()
rp.set_url("https://www.example.com/robots.txt")
rp.read()

If you already have the contents of a robots.txt file as an iterable of strings (such as when reading from a local cache or a custom HTTP client), you can bypass network retrieval by using rp.parse(lines) instead of rp.read().

2. Parsing Directives

During the parsing phase, urllib.robotparser reads the file line-by-line, stripping whitespace and discarding comments (lines beginning with #). It processes standard directives by grouping them into user-agent-specific blocks:

The parser stores these directives internally as rule objects, associating each set of Allow and Disallow path patterns with their corresponding user-agent definitions.

3. Checking URL Permissions

Once the directives are parsed into memory, the crawler queries whether a specific URL can be crawled using the can_fetch(useragent, url) method.

user_agent = "MyCustomBot"
target_url = "https://www.example.com/data/page.html"

if rp.can_fetch(user_agent, target_url):
    print("Access allowed: Proceeding with fetch.")
else:
    print("Access denied: Skipping URL.")

The can_fetch method operates using specific path comparison rules:

Handling Network Errors and Edge Cases

The parser applies default behaviors when encountering network failures: