Skip to main content

Robots Text Processor

A Python package for processing and validating robots.txt files according to the Robots Exclusion Protocol (REP) RFC.

Features

  • Parse and process robots.txt files
  • Extract user-agent rules, allow/disallow directives, and sitemaps
  • Test URLs against robots.txt rules
  • Validate robots.txt files for compliance with REP RFC
  • Generate hashes for content tracking
  • Comprehensive error and warning reporting

Installation

pip install robotstxt-package

Usage

Basic Usage

from robotstxt import robots_file

# Create a RobotsFile instance
robots = robots_file("""
User-agent: *
Allow: /
Disallow: /private/
Sitemap: https://example.com/sitemap.xml
""")

# Test a URL
result = robots.test_url("https://example.com/private/page", "*")
print(result)  # {'disallowed': True, 'matching_rule': '/private/'}

# Get sitemaps
for sitemap in robots.sitemaps:
    print(sitemap.url)

Validation

The package includes comprehensive validation of robots.txt files:

from robotstxt import robots_file

# Create a RobotsFile instance
robots = robots_file("""
User-agent: *
Allow: /
Disallow: /private/
Sitemap: https://example.com/sitemap.xml
""")

# Check for validation errors
if robots.has_errors():
    for error in robots.get_validation_errors():
        print(f"Error: {error.message} (Line {error.line_number})")

# Check for validation warnings
if robots.has_warnings():
    for warning in robots.get_validation_warnings():
        print(f"Warning: {warning.message} (Line {warning.line_number})")

The validation system checks for:

  • File size exceeding 500KB
  • UTF-8 Byte Order Mark (BOM) presence
  • Invalid characters in lines
  • Proper directive formatting (user-agent, allow, disallow, sitemap)
  • Rule block structure (user-agent directives before allow/disallow rules)
  • Valid sitemap URLs
  • Duplicate rules for the same user-agent
  • And more...

Content Tracking

The package provides hashing functionality for tracking changes:

from robotstxt import robots_file

robots = robots_file(content)

# Get content hash
print(robots.hash_raw)  # SHA-256 hash of raw content

# Get rules hash
print(robots.hash_material)  # SHA-256 hash of processed rules

# Get sitemaps hash
print(robots.hash_sitemaps)  # SHA-256 hash of sitemap URLs

Validation Rules

The package validates robots.txt files according to the following rules:

  1. File Size

    • Warning if file exceeds 500KB
    • Error if file exceeds 512KB
  2. Character Encoding

    • Error if file contains UTF-8 BOM
    • Error if file contains invalid characters
  3. Directive Format

    • Error for invalid directive format
    • Error for missing user-agent before allow/disallow rules
    • Warning for common typos in directives
  4. Rule Structure

    • Error for duplicate rule blocks
    • Warning for conflicting allow/disallow rules
    • Warning for overly broad rules
  5. Sitemaps

    • Error for invalid sitemap URLs
    • Warning for multiple sitemaps

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

compare_robots_files(robots1, robots2)

Compares two robots.txt files and generates a structured diff showing:

  • Material differences between the files
  • Per-token changes showing added and removed rules
  • Sitemap changes

The function normalizes the rules before comparison to ensure accurate diffing regardless of:

  • Rule ordering within token groups
  • Whitespace differences
  • Case sensitivity in tokens
  • Duplicate rules

Returns a dictionary containing:

  • materially_different: Boolean indicating if the files have different rules
  • token_diffs: Dictionary of differences per token
  • sitemap_changes: Dictionary of sitemap differences

Example usage

robots1 = RobotsFile(old_content) robots2 = RobotsFile(new_content)

Either way works:

diff = compare_robots_files(robots1, robots2)

or

diff = robots1.compare_with(robots2)

Example output structure:

{

"materially_different": True,

"token_diffs": {

"googlebot": {

"added": ["Allow: /new-path/", "Disallow: /private/"],

"removed": ["Disallow: /old-path/"]

},

"bingbot": {

"added": ["Disallow: /api/"],

"removed": []

}

},

"sitemap_changes": {

"added": ["https://example.com/new-sitemap.xml"],

"removed": ["https://example.com/old-sitemap.xml"]

}

}

Release files for robotstxt 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for robotstxt 1.1.2
File Size Uploaded
robotstxt-1.1.2.tar.gz 16.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for robotstxt 1.1.2
File Interpreter ABI Platform
robotstxt-1.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 34.4 kB

Release files / robotstxt-1.1.2.tar.gz

Download URL robotstxt-1.1.2.tar.gz
Size 16.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ab406b1f7d4d348403a0df1542425637c954b623102e67805391cb5de5adad54
BLAKE2b-256 checksum
How to use checksums
981c17ea5d9b8548826ed790aeebee65e78d2e4756e93c671c16570cf38f2312
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / robotstxt-1.1.2-py3-none-any.whl

Download URL robotstxt-1.1.2-py3-none-any.whl
Size 18.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
38fcb6c6dea6566ceffeb1e44c88f1015a96585084d232fc9de151dbd25da1c6
BLAKE2b-256 checksum
How to use checksums
120eadc571886b6a4c41e1382972ee853895a14dd67a606867e24c12d482e40b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

1.1.2 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0

1 release file

0.0.10

1 release file

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page