[![reqwest-badge]][reqwest] [![select-badge]][select] [![url-badge]][url] [![cat-net-badge]][cat-net]
调用get_base_url
以检索 base URL。如果文档有 base 标签,从 base 标签获取attr
。Position::BeforePath
作为 原始 URL 的 默认行为。
遍历文档中的链接,并用url::ParseOptions
和Url::parse
解析。用 reqwest
对链接做请求,并验证StatusCode
。
# #[macro_use]
# extern crate error_chain;
extern crate reqwest;
extern crate select;
extern crate url;
use std::collections::HashSet;
use url::{Url, Position};
use reqwest::StatusCode;
use select::document::Document;
use select::predicate::Name;
#
# error_chain! {
# foreign_links {
# ReqError(reqwest::Error);
# IoError(std::io::Error);
# UrlParseError(url::ParseError);
# }
# }
fn get_base_url(url: &Url, doc: &Document) -> Result<Url> {
let base_tag_href = doc.find(Name("base")).filter_map(|n| n.attr("href")).nth(0);
let base_url = base_tag_href.map_or_else(
|| Url::parse(&url[..Position::BeforePath]),
Url::parse,
)?;
Ok(base_url)
}
fn check_link(url: &Url) -> Result<bool> {
let res = reqwest::get(url.as_ref())?;
Ok(res.status() != StatusCode::NOT_FOUND)
}
fn run() -> Result<()> {
let url = Url::parse("https://www.rust-lang.org/en-US/")?;
let res = reqwest::get(url.as_ref())?;
let document = Document::from_read(res)?;
let base_url = get_base_url(&url, &document)?;
let base_parser = Url::options().base_url(Some(&base_url));
let links: HashSet<Url> = document
.find(Name("a"))
.filter_map(|n| n.attr("href"))
.filter_map(|link| base_parser.parse(link).ok())
.collect();
links
.iter()
.filter(|link| check_link(link).ok() == Some(false))
.for_each(|x| println!("{} is broken.", x));
Ok(())
}
#
# quick_main!(run);