10 个版本 (5 个重大更改)
0.7.0 | 2020年3月31日 |
---|---|
0.6.0 | 2019年1月7日 |
0.5.5 | 2018年5月10日 |
0.5.3 | 2018年2月24日 |
0.5.1 | 2017年4月8日 |
#678 in 网页编程
每月40次下载
30KB
771 行
robots_txt
robots_txt 是用 Rust 编写的轻量级 robots.txt 解析器和生成器。
无额外内容。
不稳定
实现正在进行中。
安装
Robots_txt 可在 crates.io 上获取,并可以像这样包含到您的 Cargo 启用的项目中
Cargo.toml
[dependencies]
robots_txt = "0.7"
解析和匹配路径与规则
use robots_txt::Robots;
static ROBOTS: &'static str = r#"
# robots.txt for http://www.site.com
User-Agent: *
Disallow: /cyberworld/map/ # this is an infinite virtual URL space
# Cybermapper knows where to go
User-Agent: cybermapper
Disallow:
"#;
fn main() {
let robots = Robots::from_str(ROBOTS);
let matcher = SimpleMatcher::new(&robots.choose_section("NoName Bot").rules);
assert!(matcher.check_path("/some/page"));
assert!(matcher.check_path("/cyberworld/welcome.html"));
assert!(!matcher.check_path("/cyberworld/map/object.html"));
let matcher = SimpleMatcher::new(&robots.choose_section("Mozilla/5.0; CyberMapper v. 3.14").rules);
assert!(matcher.check_path("/some/page"));
assert!(matcher.check_path("/cyberworld/welcome.html"));
assert!(matcher.check_path("/cyberworld/map/object.html"));
}
构建和渲染
main.rs
extern crate robots_txt;
use robots_txt::Robots;
fn main() {
let robots1 = Robots::builder()
.start_section("cybermapper")
.disallow("")
.end_section()
.start_section("*")
.disallow("/cyberworld/map/")
.end_section()
.build();
let conf_base_url: Url = "https://example.com/".parse().expect("parse domain");
let robots2 = Robots::builder()
.host(conf_base_url.domain().expect("domain"))
.start_section("*")
.disallow("/private")
.disallow("")
.crawl_delay(4.5)
.request_rate(9, 20)
.sitemap("http://example.com/sitemap.xml".parse().unwrap())
.end_section()
.build();
println!("# robots.txt for http://cyber.example.com/\n\n{}", robots1);
println!("# robots.txt for http://example.com/\n\n{}", robots2);
}
结果如下
# robots.txt for http://cyber.example.com/
User-agent: cybermapper
Disallow:
User-agent: *
Disallow: /cyberworld/map/
# robots.txt for http://example.com/
User-agent: *
Disallow: /private
Disallow:
Crawl-delay: 4.5
Request-rate: 9/20
Sitemap: http://example.com/sitemap.xml
Host: example.com
替代方案
- messense/robotparser-rs Rust 的 robots.txt 解析器
许可证
许可方式为以下之一
- Apache 许可证 2.0 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT 许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT),您可选其一。
贡献
除非您明确表示,否则根据 Apache-2.0 许可证定义的,任何有意提交以包含在本作品中的贡献,都将按上述方式双许可,不附加任何额外条款或条件。
依赖项
~1.5MB
~50K SLoC