7 个版本 (1 个稳定版)
| 1.0.0 | 2020 年 8 月 8 日 | 
|---|---|
| 0.4.0 | 2020 年 4 月 30 日 | 
| 0.3.0 | 2020 年 4 月 30 日 | 
| 0.2.1 | 2020 年 4 月 29 日 | 
| 0.1.1 | 2020 年 4 月 27 日 | 
#27 在 #extracting
每月 23 次下载
被  4 个 包使用
26KB
277 行
html-extractor
一个用于从 HTML 中提取数据的 Rust 包。
示例
从 HTML 中提取简单值
use html_extractor::{html_extractor, HtmlExtractor};
html_extractor! {
    #[derive(Debug, PartialEq)]
    Foo {
        foo: usize = (text of "#foo"),
    }
}
fn main() {
    let input = r#"
        <div id="foo">1</div>
    "#;
    let foo = Foo::extract_from_str(input).unwrap();
    assert_eq!(foo, Foo { foo: 1 });
}
从 HTML 中提取集合
use html_extractor::{html_extractor, HtmlExtractor};
html_extractor! {
    #[derive(Debug, PartialEq)]
    Foo {
        foo: Vec<usize> = (text of ".foo", collect),
    }
}
fn main() {
    let input = r#"
        <div class="foo">1</div>
        <div class="foo">2</div>
        <div class="foo">3</div>
        <div class="foo">4</div>
    "#;
    let foo = Foo::extract_from_str(input).unwrap();
    assert_eq!(foo, Foo { foo: vec![1, 2, 3, 4] });
}
使用正则表达式提取
use html_extractor::{html_extractor, HtmlExtractor};
html_extractor! {
    #[derive(Debug, PartialEq)]
    Foo {
        (foo: usize,) = (text of "#foo", capture with "^foo=(.*)$"),
    }
}
fn main() {
    let input = r#"
        <div id="foo">foo=1</div>
    "#;
    let foo = Foo::extract_from_str(input).unwrap();
    assert_eq!(foo, Foo { foo: 1 });
}
更新日志
v0.4.0
- 添加 存在 ..目标指定器
v0.3.0
- 添加解析器指定器
- 添加 inner_html目标指定器
- 修改提取文本节点时去除两端空格的行为。
- 修复错误信息
v0.2.1
- 修复 rust 标准库的内部使用
v0.2.0
- 将 "收集指定器" 重命名为 "收集器指定器"
- 添加 "可选" 收集器
v0.1.1
- 修复文档中的链接
依赖
~5–7MB
~130K SLoC