Skip to main content

HtmlParser

Struct HtmlParser 

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

统一的 HTML 解析器,封装文档解析与内容提取的全流程。

通过 ParserConfig 控制提取的内容类型和行为。

§示例

let parser = HtmlParser::new();
let result = parser.parse("<html><body><p>Hello</p></body></html>")?;
println!("{}", result.text.cleaned_text);

Implementations§

Source§

impl HtmlParser

Source

pub fn new() -> Self

使用默认配置创建解析器。

Source

pub fn with_config(config: ParserConfig) -> Self

使用自定义配置创建解析器。

Source

pub fn with_base_url(url: Url) -> Self

使用指定的基础 URL 创建解析器。

Source

pub fn set_base_url(&mut self, url: Url)

设置基础 URL,用于解析相对链接和图片地址。

Source

pub fn config(&self) -> &ParserConfig

获取当前配置的引用。

Source

pub fn config_mut(&mut self) -> &mut ParserConfig

获取当前配置的可变引用。

Source

pub fn parse(&self, html: &str) -> ParserResult<ParsedContent>

解析完整的 HTML 文档,返回结构化内容。

自动计算解析统计信息,并根据配置决定提取哪些内容类型。

Source

pub fn parse_fragment(&self, html: &str) -> ParserResult<ParsedContent>

解析 HTML 片段(不包含 <html>/<body> 包装)。

适用于解析页面中的局部 HTML 内容。

Source

pub fn extract_metadata(&self, html: &str) -> ParserResult<PageMetadata>

提取页面元数据(标题、描述、OG、Twitter Card、结构化数据等)。

Source

pub fn extract_text(&self, html: &str) -> ParserResult<TextContent>

提取页面文本内容,含清洗、可读性评分。

Source

pub fn extract_headings(&self, html: &str) -> Vec<Heading>

提取所有标题(h1 - h6)。

提取所有链接。

Source

pub fn extract_images(&self, html: &str) -> Vec<Image>

提取所有图片,自动解析相对地址为绝对地址。

Source

pub fn extract_lists(&self, html: &str) -> Vec<ListContent>

提取所有列表(有序、无序、定义列表)。

Source

pub fn extract_tables(&self, html: &str) -> Vec<TableContent>

提取所有表格。

Source

pub fn extract_code_blocks(&self, html: &str) -> Vec<CodeBlock>

提取所有代码块。

Source

pub fn extract_quotes(&self, html: &str) -> Vec<Quote>

提取所有引用(blockquote)。

Source

pub fn extract_structured_data(&self, html: &str) -> Vec<StructuredData>

提取所有结构化数据(JSON-LD + Microdata)。

Source

pub fn resolve_url(&self, href: &str) -> Option<String>

将相对路径或协议相对 URL 解析为绝对 URL。

需要解析器已配置 base_url,否则返回 None

Source

pub fn has_base_url(&self) -> bool

检查是否已配置基础 URL。

Source

pub fn base_url(&self) -> Option<&Url>

获取当前基础 URL 的引用。

Trait Implementations§

Source§

impl Clone for HtmlParser

Source§

fn clone(&self) -> HtmlParser

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 HtmlParser

Source§

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

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

impl Default for HtmlParser

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.