17 releases

0.8.0 Nov 7, 2020
0.7.5 Dec 25, 2019
0.7.4 Oct 1, 2019
0.6.0 Feb 12, 2019
0.3.0 Dec 12, 2018

#2810 in Parser implementations

Download history 11/week @ 2024-06-15 4/week @ 2024-06-22 26/week @ 2024-07-13 18/week @ 2024-07-20 41/week @ 2024-07-27 1/week @ 2024-08-03 39/week @ 2024-08-10 1/week @ 2024-08-17 11/week @ 2024-08-24 7/week @ 2024-08-31 47/week @ 2024-09-21 21/week @ 2024-09-28

68 downloads per month
Used in 5 crates

MIT license

19KB
507 lines

unhtml

Stable Test Rust Docs Crate version Download License: MIT

There are two trait in crate unhtml

  • FromHtml

The only method of FromHtml you should care about is fn from_html(html: &str) -> Result<Self, Error> and this method is implemented for all types implemented FromStr<E, T>

impl<E, T> FromHtml for T
    where E: failure::Fail,
          T: FromStr<Err=E> {
    fn from_html(html: &str) -> Result<Self, Error> {
        Ok(T::from_str(html.trim())?)
    }
}

You can implement FromHtml automatically for struct by crate unhtml_derive Crate version

  • VecFromHtml

VecFromHtml is implemented for Vec<T> where T: FromHtml by default

impl<T> VecFromHtml for Vec<T>
    where T: FromHtml {
    type Elem = T;
}

As FromHtml is implemented for u8 by default

use unhtml::scraper::Html;
use unhtml::VecFromHtml;
let html = Html::parse_fragment(r#"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div id="test">
        <a href="1"></a>
        <a href="2"></a>
        <a href="3"></a>
    </div>
</body>
</html>
"#);
let results = Vec::<u8>::from_attr("#test > a", "href", html.root_element()).unwrap();
assert_eq!(1u8, results[0]);
assert_eq!(2u8, results[1]);
assert_eq!(3u8, results[2]);

Dependencies

~3.5–8.5MB
~83K SLoC