Skip to main content

UrlRule

Struct UrlRule 

Source
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

Source

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"));
Source

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"));
Source

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"));
Source

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§

Source§

impl Clone for UrlRule

Source§

fn clone(&self) -> UrlRule

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UrlRule

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for UrlRule

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.