Check out other spire projects here.
The implementation of the robots.txt (or URL exclusion) protocol in the Rust
programming language, following RFC 9309, with the support of the
crawl-delay and sitemap extensions.
parserto enablerobotxt::{Robots}. Enabled by default.builderto enablerobotxt::{RobotsBuilder, GroupBuilder}. Enabled by default.serdeto enableserde::{Deserialize, Serialize}implementations, allowing the caching of parsed rules.
- parse the rules that apply to the crawler:
use robotxt::Robots;
let txt = b"
User-Agent: foobot
Disallow: *
Allow: /example/
Disallow: /example/nope.txt
";
let r = Robots::parse(txt, "foobot");
assert!(r.is_allowed("/example/yeah.txt"));
assert!(!r.is_allowed("/example/nope.txt"));
assert!(!r.is_allowed("/invalid/path.txt"));- match the crawler's name by prefix instead of exactly, so a
foobotgroup also applies to thefoobot-imagescrawler:
use robotxt::{Agent, Robots};
let txt = b"
User-Agent: foobot
Disallow: /
";
let r = Robots::parse(txt, Agent::Prefix("foobot-images"));
assert!(!r.is_allowed("/"));- build a new
robots.txtfile:
use std::time::Duration;
use robotxt::RobotsBuilder;
let txt = RobotsBuilder::new()
.header("Robots.txt: Start")
.group(["foobot"], |u| {
u.crawl_delay(Duration::from_secs(5))
.header("Rules for Foobot: Start")
.allow("/example/yeah.txt")
.disallow("/example/nope.txt")
.footer("Rules for Foobot: End")
})
.group(["barbot", "nombot"], |u| {
u.crawl_delay(Duration::from_secs(2))
.disallow("/example/yeah.txt")
.disallow("/example/nope.txt")
})
.sitemap("https://example.com/sitemap_1.xml".parse().unwrap())
.sitemap("https://example.com/sitemap_2.xml".parse().unwrap())
.footer("Robots.txt: End");
println!("{txt}");- Request for Comments: 9309 on RFC-Editor.com
- Introduction to Robots.txt on Google.com
- How Google interprets Robots.txt on Google.com
- What is Robots.txt file on Moz.com
- Only
User-Agent,Allow,Disallow,Crawl-DelayandSitemapare understood. Other lines, e.g. the non-standardHostorClean-Paramdirectives, are ignored.