The robotspy Python module implements a parser for robots.txt files. The recommended class to use is
robots.RobotsParser.
A thin facade robots.RobotFileParser can also be used as a substitute for urllib.robotparser.RobotFileParser,
available in the Python standard library. The class robots.RobotFileParser exposes an API that is mostly compatible
with urllib.robotparser.RobotFileParser.
The main reasons for this rewrite are the following:
It was initially intended to experiment with parsing robots.txt files for a link checker project (not implemented yet).
It does not try to be compliant with commonly accepted directives that are not in the current specs such as request-rate
and crawl-delay, but it currently supports sitemaps.
It satisfies the same tests as the Google Robots.txt Parser, except for some custom behaviors specific to Google Robots.
To use the robots command line tool (CLI) in a Docker container, read the following section Docker Image.
To install robotspy globally as a tool on your system with pipx skip to the Global Installation section.
If you are interested in using robotspy in a local Python environment or as a library, skip to section Module Installation.
# Used by NetworkTestCase in Lib/test/test_robotparser.py
User-agent: Nutch
Disallow: /
Allow: /brian/
User-agent: *
Disallow: /webstats/
The following examples demonstrate how to use the robots command line with the Docker container:
$ # Example 1: User agent "Johnny" is allowed to access path "/"
$ docker run --rm andreburgaud/robotspy http://www.pythontest.net/elsewhere/robots.txt Johnny /
user-agent 'Johnny' with path '/': ALLOWED
$ # Example 2: User agent "Nutch" is not allowed to access path "/brian"
$ docker run --rm andreburgaud/robotspy http://www.pythontest.net/elsewhere/robots.txt Nutch /brian
user-agent 'Nutch' with path '/brian': DISALLOWED
$ # Example 3: User agent "Johnny" is not allowed to access path "/webstats/"
docker run --rm andreburgaud/robotspy http://www.pythontest.net/elsewhere/robots.txt Johnny /webstats/
user-agent 'Johnny' with path '/webstats/': DISALLOWED
The arguments are the following:
Location of the robots.txt file (http://www.pythontest.net/elsewhere/robots.txt)
User agent name (Johnny)
Path or URL (/)
Without any argument, robots displays the help:
docker run --rm andreburgaud/robotspy
usage: robots <robotstxt> <useragent> <path>
Shows whether the given user agent and path combination are allowed or disallowed by the given robots.txt file.
positional arguments:
robotstxt robots.txt file path or URL
useragent User agent name
path Path or URI
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
To use the CLI robots as a global tools, continue to the following section. If you want to use robotspy as a Python module, skip to Module Installation.
Global Installation with pipx
If you only want to use the command line tool robots, you may want to use pipx to install it as a global tool on your system.
To install robotspy using pipx execute the following command:
$ pipx install robotspy
When robotspy is installed globally on your system, you can invoke it from any folder locations. For example, you can execute:
$ robots --version
robots 0.8.0
You can see more detailed usages in section Usage.
Module Installation
Note: Python 3.8.x or 3.9.x required
You preferably want to install the robotspy package after creating a Python virtual environment,
in a newly created directory, as follows:
The robotspy package can be imported as a module and also exposes an executable, robots, invocable with
python -m. If installed globally with pipx, the command robots can be invoked from any folders. The usage examples in the following section use the command robots, but you can also substitute it with python -m robots in a virtual environment.
Execute the Tool
After installing robotspy, you can validate the installation by running the following command:
$ robots --help
usage: robots <robotstxt> <useragent> <path>
Shows whether the given user agent and path combination are allowed or disallowed by the given robots.txt file.
positional arguments:
robotstxt robots.txt file path or URL
useragent User agent name
path Path or URI
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
Updated the parser to behave like the Google robots parser. It now handles the product token in the user-agent line up to the last correct character instead of discarding it. See issue #209 for more details.