Blazingly fast robots.txt parser written in Rust - 6x faster than reppy
Project description
fastrobots
A blazingly fast robots.txt parser written in Rust with Python bindings. A modern replacement for reppy with extended features.
Features
- Blazingly Fast: Written in Rust, 1M+ parses/sec, 8M+ URL checks/sec
- Modern API: Python 3.10+ with full type hints
- Async Support: Native async/await for HTTP fetching
- Caching: LRU caching with TTL support
- CLI: Command-line tool for quick checks
- RFC Compliant: Supports Allow, Disallow, Crawl-delay, Sitemap, wildcards
Installation
pip install fastrobots
Or with uv:
uv add fastrobots
Quick Start
from fastrobots import Robots
# Parse robots.txt content
robots = Robots.parse("""
User-agent: *
Disallow: /private/
Allow: /public/
Crawl-delay: 1
Sitemap: https://example.com/sitemap.xml
""")
# Check if a path is allowed
robots.allowed("/public/page", "MyBot") # True
robots.allowed("/private/secret", "MyBot") # False
# Get crawl delay
robots.crawl_delay("MyBot") # 1.0
# Get sitemaps
robots.sitemaps # ["https://example.com/sitemap.xml"]
Fetching from URLs
from fastrobots import fetch, fetch_async
# Sync fetching
robots = fetch("https://example.com")
robots.allowed("/path", "MyBot")
# Async fetching
robots = await fetch_async("https://example.com")
Caching
from fastrobots import RobotsCache, AgentCache
# Cache multiple domains
cache = RobotsCache(capacity=100, default_ttl=3600)
allowed = cache.allowed("https://example.com/path", "MyBot")
# Or async
allowed = await cache.allowed_async("https://example.com/path", "MyBot")
# Single-agent cache (more memory efficient)
cache = AgentCache(agent="MyBot", capacity=100)
allowed = cache.allowed("https://example.com/path")
Agent API
from fastrobots import Robots
robots = Robots.parse(content)
agent = robots.agent("Googlebot")
agent.allowed("/path") # True/False
agent.delay # Crawl delay in seconds or None
agent.name # "Googlebot"
CLI Usage
# Check if a URL is allowed
fastrobots check https://example.com/path --agent MyBot
# Parse and display robots.txt
fastrobots parse https://example.com
# Output as JSON
fastrobots parse https://example.com --json
# Benchmark performance
fastrobots benchmark
fastrobots benchmark /path/to/robots.txt
Wildcard Support
Supports * and $ wildcards as per Google's robots.txt specification:
robots = Robots.parse("""
User-agent: *
Disallow: /*.pdf$
Disallow: /search?*q=
""")
robots.allowed("/doc.pdf", "*") # False
robots.allowed("/doc.pdf.bak", "*") # True ($ means end)
robots.allowed("/search?q=test", "*") # False
Performance
fastrobots is designed for high-performance web crawling. Benchmarked against reppy in Docker (Ubuntu 20.04):
| Library | Parse Speed | Check Speed |
|---|---|---|
| fastrobots v0.3.0 | 1,022,875 /sec | 8,025,846 /sec |
| reppy | 159,968 /sec | 2,609,011 /sec |
| Speedup | 6.4x faster | 3.1x faster |
Why fastrobots is faster
- Arena allocation: All path data stored in a single contiguous memory block
- Packed structs: 8-byte rule structs (8 rules per CPU cache line)
- Zero-allocation hot path: No heap allocations during URL checking
- Lock-free design: Immutable after parsing, no synchronization overhead
- Custom SIMD matcher: Uses
memchrfor fast wildcard matching, no regex engine
Additional advantages over reppy
- Actually installs: reppy fails to compile on modern systems (g++ incompatibility)
- Pure Rust: No C++ dependencies, just
cargo build - Thread-safe: Safe to share across threads without locks
API Reference
Robots
Robots.parse(content: str) -> Robots- Parse robots.txt contentrobots.allowed(path: str, user_agent: str) -> bool- Check if path is allowedrobots.crawl_delay(user_agent: str) -> float | None- Get crawl delayrobots.agent(user_agent: str) -> Agent- Get Agent objectrobots.sitemaps: list[str]- List of sitemap URLsrobots.user_agents: list[str]- List of user-agents
Agent
agent.allowed(path: str) -> bool- Check if path is allowedagent.delay: float | None- Crawl delayagent.name: str- User-agent name
RobotsCache
RobotsCache(capacity=100, default_ttl=3600)- Create cachecache.allowed(url, user_agent) -> bool- Check with cachingcache.allowed_async(url, user_agent) -> bool- Async checkcache.clear()- Clear cache
fetch / fetch_async
fetch(url, user_agent=..., timeout=10.0) -> Robots- Fetch robots.txtfetch_async(url, ...) -> Robots- Async fetch
Utility Functions
robots_url(url: str) -> str- Get robots.txt URL for any URLurl_path(url: str) -> str- Extract path from URL
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastrobots-0.3.0.tar.gz.
File metadata
- Download URL: fastrobots-0.3.0.tar.gz
- Upload date:
- Size: 39.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c7c2ba4eaf4f3a93c3df21bbad2bfa5b6fab345e6e612b8c1e8a2fc3c2c4b88
|
|
| MD5 |
ddca615351b44f1a9fa1105f960dd4ff
|
|
| BLAKE2b-256 |
e830c68e8b6c9c561aa90d2b95e96e1258b500f0360652fa51bb150eec1c8e63
|
File details
Details for the file fastrobots-0.3.0-cp312-cp312-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: fastrobots-0.3.0-cp312-cp312-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 354.6 kB
- Tags: CPython 3.12, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3fd150d47e95ad1ed23928b03093522c9f43a2777d253e696418f92feb285cd1
|
|
| MD5 |
8e61abbc788e57c01a9923181d55961d
|
|
| BLAKE2b-256 |
eb0ab8f1a7f101de44c4b910c2afb116b872a1c51b0b20a96b806a086520d34f
|