Protego is a pure-Python robots.txt parser. It implements the parsing and
URL matching rules of RFC 9309, and additionally supports the
Crawl-delay, Request-rate, Visit-time and Host extensions.
Fetching robots.txt is up to you, and so are the parts of RFC 9309 that
govern it, such as the handling of HTTP status codes and redirects, caching, and
imposing a parsing limit.
To install Protego, simply use pip:
pip install protego
>>> from protego import Protego
>>> robotstxt = """
... User-agent: *
... Disallow: /
... Allow: /about
... Allow: /account
... Disallow: /account/contact$
... Disallow: /account/*/profile
... Crawl-delay: 4
... Request-rate: 10/1m # 10 requests every 1 minute
...
... Sitemap: http://example.com/sitemap-index.xml
... Host: http://example.co.in
... """
>>> rp = Protego.parse(robotstxt)
>>> rp.can_fetch("http://example.com/profiles", "mybot")
False
>>> rp.can_fetch("http://example.com/about", "mybot")
True
>>> rp.can_fetch("http://example.com/account", "mybot")
True
>>> rp.can_fetch("http://example.com/account/myuser/profile", "mybot")
False
>>> rp.can_fetch("http://example.com/account/contact", "mybot")
False
>>> rp.crawl_delay("mybot")
4.0
>>> rp.request_rate("mybot")
RequestRate(requests=10, seconds=60, start_time=None, end_time=None)
>>> list(rp.sitemaps)
['http://example.com/sitemap-index.xml']
>>> rp.preferred_host
'http://example.co.in'Using Protego with Requests:
>>> from protego import Protego
>>> import requests
>>> r = requests.get("https://google.com/robots.txt")
>>> rp = Protego.parse(r.text)
>>> rp.can_fetch("https://google.com/search", "mybot")
False
>>> rp.can_fetch("https://google.com/search/about", "mybot")
True
>>> list(rp.sitemaps)
['https://www.google.com/sitemap.xml']The following table compares Protego to the most popular robots.txt parsers
implemented in Python. Performance is the speed difference against Protego, so
a positive value means faster than Protego. It is measured over the
robots.txt of 100 of the most visited websites: the time taken to check the
URLs their homepages link to, and the time taken to parse the files themselves.
| Protego | RobotFileParser | robotspy | Robotexclusionrulesparser | |
|---|---|---|---|---|
| Version tested | Python 3.14.7 | 0.13.0 | 1.7.1 | |
| Reference specification | RFC 9309 | Martijn Koster's 1996 draft | RFC 9309 | Martijn Koster's 1996 draft |
| Wildcard support | ✓ | ✓ | ✓ | ✓ |
| Length-based precedence | ✓ | ✓ | ✓ | |
| Crawl-delay | ✓ | ✓ | ||
| Request-rate | ✓ | |||
| Visit-time | ✓ | |||
| Sitemaps | ✓ | ✓ | ✓ | ✓ |
| Host | ✓ | |||
| Matching performance | -39% | -56% | -94% | |
| Parsing performance | -55% | +110% | +150% |
Class protego.Protego:
sitemaps{list_iterator} A list of sitemaps specified inrobots.txt.preferred_host{string} Preferred host specified inrobots.txt.
parse(robotstxt_body)Parserobots.txtand return a new instance ofprotego.Protego.can_fetch(url, user_agent)Return True if the user agent can fetch the URL, otherwise returnFalse.user_agent may be a product token, such as
"mybot", or a wholeUser-Agentheader value, such as"Mozilla/5.0 (compatible; mybot/1.0)"; a group applies when its product token appears in user_agent at a token boundary.crawl_delay(user_agent)Return the crawl delay specified for the user agent as a float. If nothing is specified, returnNone.request_rate(user_agent)Return the request rate specified for the user agent as a named tupleRequestRate(requests, seconds, start_time, end_time). If nothing is specified, returnNone.visit_time(user_agent)Return the visit time specified for the user agent as a named tupleVisitTime(start_time, end_time). If nothing is specified, returnNone.