Skip to main content

LinkExtractor

Struct LinkExtractor 

Source
pub struct LinkExtractor { /* private fields */ }
Expand description

基于 DOM 结构聚类 + 多信号打分的文章链接提取器。

算法流程:

  1. 提取页面中所有 <a[href]> 元素
  2. 对每个链接计算 DOM 路径签名(dom_path_signature),按签名聚类
  3. 对每个候选链接计算 8 个信号的加权评分
  4. 同一 URL 保留最高分,过滤低于阈值的链接,按分数降序排列

§示例

use crawlkit_parser::{LinkExtractor, ExtractorConfig};

let html = r#"
<html><body>
    <div class="list">
        <a href="/2024/01/15/story-one">第一篇新闻标题</a>
        <a href="/2024/01/16/story-two">第二篇新闻标题</a>
        <a href="/2024/01/17/story-three">第三篇新闻标题</a>
        <a href="/2024/01/18/story-four">第四篇新闻标题</a>
    </div>
</body></html>
"#;
let extractor = LinkExtractor::new(ExtractorConfig::default());
let results = extractor.extract(html, "https://example.com/");
assert_eq!(results.len(), 4);

Implementations§

Source§

impl LinkExtractor

Source

pub fn new(config: ExtractorConfig) -> Self

创建一个新的提取器。

内部使用默认的 UrlRule,可通过 with_url_rule 覆盖。

use crawlkit_parser::{LinkExtractor, ExtractorConfig};

let extractor = LinkExtractor::new(ExtractorConfig::default());
Source

pub fn with_url_rule(self, rule: UrlRule) -> Self

设置自定义 UrlRule,覆盖默认规则。

use crawlkit_parser::{LinkExtractor, ExtractorConfig, UrlRule};

let rule = UrlRule::default().with_include(r"/press/");
let extractor = LinkExtractor::new(ExtractorConfig::default())
    .with_url_rule(rule);
Source

pub fn extract(&self, html: &str, base_url: &str) -> Vec<ExtractedLink>

从 HTML 中提取候选文章链接,按 score 从高到低排列。

§参数
  • html: 页面 HTML 源码
  • base_url: 页面 URL,用于相对路径解析和站外链接判定
§返回值

按评分降序排列的文章链接列表,已过滤低于 score_threshold 的链接。

use crawlkit_parser::{LinkExtractor, ExtractorConfig};

let html = r#"
<ul>
    <li><a href="/news/2025/01/01/story-a">2025年第一篇新闻</a></li>
    <li><a href="/news/2025/01/02/story-b">2025年第二篇新闻</a></li>
    <li><a href="/news/2025/01/03/story-c">2025年第三篇新闻</a></li>
    <li><a href="/news/2025/01/04/story-d">2025年第四篇新闻</a></li>
</ul>
"#;
let extractor = LinkExtractor::new(ExtractorConfig::default());
let results = extractor.extract(html, "https://example.com/");
assert!(results.len() >= 4);
for link in &results {
    println!("{} score={}", link.url, link.score);
}

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> 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, 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.