Expand description
§robotxt
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.
§Features
parserto enablerobotxt::{Robots}. Enabled by default.builderto enablerobotxt::{RobotsBuilder, GroupBuilder}. Enabled by default.serdeto enableserde::{Deserialize, Serialize}implementations, allowing the caching of parsed rules.
§Examples
- 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}");§Links
- 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
§Notes
- Only
User-Agent,Allow,Disallow,Crawl-DelayandSitemapare understood. Other lines, e.g. the non-standardHostorClean-Paramdirectives, are ignored.
Re-exports§
pub use url;
Structs§
- Group
Builder builder - The builder of a single
User-Agentgroup. - Robots
parser - The rules of a
robots.txtfile that apply to a single crawler. - Robots
Builder builder - The builder of
robots.txtfiles. - Rule
parser - A single
AlloworDisallowrule of the group that applies to the crawler.
Enums§
- Access
Result parser - The result of an attempt to fetch the
robots.txtfile, as described in RFC 9309, section 2.3.1. - Agent
parser - The crawler’s name and how it is matched against
User-Agentvalues. - Error
- Failure to derive the
robots.txtlocation from a URL.
Constants§
- ANY_
AGENT - The
User-Agentvalue of the group that applies to every crawler without a group of its own. - BYTE_
LIMIT parser - The maximum number of bytes of a
robots.txtfile that are parsed.
Functions§
- robots_
url - Returns the location of the
robots.txtfile that governs the URL.