pub struct UrlRule {
pub name: String,
pub include_patterns: Vec<Regex>,
pub exclude_patterns: Vec<Regex>,
pub link_selector: Option<String>,
}Expand description
URL 匹配规则,用于判断一个链接是否为文章/新闻链接。
内部维护两套正则模式列表:
- 包含模式(include):命中任意一条即视为文章链接
- 排除模式(exclude):命中任意一条即跳过,优先级高于包含模式
默认内置 30+ 条常用文章路径规则(日期路径、关键词、CMS 模式等),
覆盖绝大多数政府、智库、新闻类站点。对于规则未能覆盖的站点,
可通过链式调用 with_include / with_exclude 扩展。
§示例
use crawlkit_parser::UrlRule;
let rule = UrlRule::default();
// 日期路径匹配
assert!(rule.is_article_url("https://example.com/2024/01/15/hello-world"));
// 关键词路径匹配
assert!(rule.is_article_url("https://example.com/news/breaking-story"));
// 静态资源被排除
assert!(!rule.is_article_url("https://example.com/image.jpg"));
// 标签页面被排除
assert!(!rule.is_article_url("https://example.com/tag/rust"));
// 扩展自定义规则
let rule = UrlRule::default()
.with_include(r"/my-section/")
.with_exclude(r"print=true")
.with_selector("div.content a");
assert!(rule.is_article_url("https://example.com/my-section/foo"));
assert!(!rule.is_article_url("https://example.com/my-section/foo?print=true"));Fields§
§name: String§include_patterns: Vec<Regex>包含模式列表 —— 命中任意一条即为文章链接
exclude_patterns: Vec<Regex>排除模式列表 —— 命中任意一条即跳过
link_selector: Option<String>可选的 CSS 选择器,限制只在页面特定区域内查找链接(如 “div.news-list a”)
Implementations§
Source§impl UrlRule
impl UrlRule
Sourcepub fn is_article_url(&self, url: &str) -> bool
pub fn is_article_url(&self, url: &str) -> bool
判断一个 URL 是否符合文章规则。
先检查所有排除模式,任一匹配则返回 false;
再检查所有包含模式,任一匹配则返回 true;
均不匹配则返回 false。
use crawlkit_parser::UrlRule;
let rule = UrlRule::default();
assert!(rule.is_article_url("https://example.com/news/story"));
assert!(!rule.is_article_url("https://example.com/style.css"));
assert!(!rule.is_article_url("https://example.com/login"));Sourcepub fn with_include(self, pattern: &str) -> Self
pub fn with_include(self, pattern: &str) -> Self
添加一条包含模式(链式调用)。
use crawlkit_parser::UrlRule;
let rule = UrlRule::default()
.with_include(r"/custom-section/");
assert!(rule.is_article_url("https://example.com/custom-section/foo"));Sourcepub fn with_exclude(self, pattern: &str) -> Self
pub fn with_exclude(self, pattern: &str) -> Self
添加一条排除模式(链式调用)。
use crawlkit_parser::UrlRule;
let rule = UrlRule::default()
.with_exclude(r"print=true");
assert!(!rule.is_article_url("https://example.com/news/story?print=true"));Sourcepub fn with_selector(self, selector: &str) -> Self
pub fn with_selector(self, selector: &str) -> Self
设置 CSS 选择器,限定链接查找范围(链式调用)。
设置后 Extractor 仅在匹配该选择器的元素内部查找 <a> 标签。
use crawlkit_parser::UrlRule;
let rule = UrlRule::default()
.with_selector("div.article-list");
assert_eq!(rule.link_selector.as_deref(), Some("div.article-list"));Trait Implementations§
Auto Trait Implementations§
impl Freeze for UrlRule
impl RefUnwindSafe for UrlRule
impl Send for UrlRule
impl Sync for UrlRule
impl Unpin for UrlRule
impl UnsafeUnpin for UrlRule
impl UnwindSafe for UrlRule
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more