Skip to main content

Html

Struct Html 

Source
pub struct Html<'s> { /* private fields */ }
Expand description

解析好的文档。借用源字符串(输入含 \r 或 NUL 时持有规范化后的副本)。

Implementations§

Source§

impl<'s> Html<'s>

Source

pub fn parse_with(html: &'s str, opts: &ParseOptions<'_>) -> Html<'s>

按选项解析:关闭脚本、在给定上下文元素里解析片段。

Source

pub fn parse(html: &'s str) -> Html<'s>

解析整个 HTML 文档。永不失败:任何输入都会得到一棵树。

Examples found in repository?
examples/parse_bench.rs (line 30)
6fn main() {
7    let mut args = std::env::args().skip(1);
8    let manifest = args
9        .next()
10        .expect("usage: parse_bench <manifest.jsonl> [rounds]");
11    let rounds: usize = args.next().and_then(|s| s.parse().ok()).unwrap_or(5);
12    let pages: Vec<String> = std::fs::read_to_string(&manifest)
13        .unwrap()
14        .lines()
15        .filter_map(|l| {
16            let i = l.find("\"path\"")?;
17            let rest = &l[i + 6..];
18            let s = rest.find('"')? + 1;
19            let e = s + rest[s..].find('"')?;
20            std::fs::read(&rest[s..e]).ok()
21        })
22        .map(|b| String::from_utf8_lossy(&b).into_owned())
23        .collect();
24    let mut best = f64::MAX;
25    let mut nodes = 0;
26    for _ in 0..rounds {
27        let t = Instant::now();
28        nodes = 0;
29        for p in &pages {
30            nodes += std::hint::black_box(pagesift_html::Html::parse(p)).len();
31        }
32        let ms = t.elapsed().as_secs_f64() * 1e3;
33        best = best.min(ms);
34        print!("{ms:.1} ");
35    }
36    println!("| pages={} nodes={nodes} best_ms={best:.1}", pages.len());
37}
Source

pub fn parse_with_scratch(html: &'s str, scratch: &mut ParseScratch) -> Html<'s>

Parse while reusing node, attribute, and decoded-text allocations from the preceding document handled by this worker.

Source

pub fn root(&self) -> NodeRef<'_>

文档根节点(不是 <html> 元素,是它的父节点)。

Source

pub fn len(&self) -> usize

节点总数。节点 id 的范围是 0..len(),可以用来开按节点下标索引的数组。

Examples found in repository?
examples/parse_bench.rs (line 30)
6fn main() {
7    let mut args = std::env::args().skip(1);
8    let manifest = args
9        .next()
10        .expect("usage: parse_bench <manifest.jsonl> [rounds]");
11    let rounds: usize = args.next().and_then(|s| s.parse().ok()).unwrap_or(5);
12    let pages: Vec<String> = std::fs::read_to_string(&manifest)
13        .unwrap()
14        .lines()
15        .filter_map(|l| {
16            let i = l.find("\"path\"")?;
17            let rest = &l[i + 6..];
18            let s = rest.find('"')? + 1;
19            let e = s + rest[s..].find('"')?;
20            std::fs::read(&rest[s..e]).ok()
21        })
22        .map(|b| String::from_utf8_lossy(&b).into_owned())
23        .collect();
24    let mut best = f64::MAX;
25    let mut nodes = 0;
26    for _ in 0..rounds {
27        let t = Instant::now();
28        nodes = 0;
29        for p in &pages {
30            nodes += std::hint::black_box(pagesift_html::Html::parse(p)).len();
31        }
32        let ms = t.elapsed().as_secs_f64() * 1e3;
33        best = best.min(ms);
34        print!("{ms:.1} ");
35    }
36    println!("| pages={} nodes={nodes} best_ms={best:.1}", pages.len());
37}
Source

pub fn is_empty(&self) -> bool

Source

pub fn get(&self, id: NodeId) -> NodeRef<'_>

Source

pub fn body(&self) -> NodeRef<'_>

第一个 <body>,没有时返回根节点。

Source§

impl<'a> Html<'a>

Source

pub fn select<'s>(&'a self, sel: &'s Selector) -> Select<'a, 's> ⓘ

文档里匹配选择器的所有元素,按文档顺序。

Trait Implementations§

Source§

impl Debug for Html<'_>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'s> Freeze for Html<'s>

§

impl<'s> RefUnwindSafe for Html<'s>

§

impl<'s> Send for Html<'s>

§

impl<'s> Sync for Html<'s>

§

impl<'s> Unpin for Html<'s>

§

impl<'s> UnsafeUnpin for Html<'s>

§

impl<'s> UnwindSafe for Html<'s>

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.