I am having problem scraping if an HTML element’s class attribute contains white spaces. I am using the scraper crate. Here are the following code to reproduce the problem:
use scraper::{Html, Selector};
fn main() {
let html = r#"
<!DOCTYPE html>
<meta charset="utf-8">
<title>Hello, world!</title>
<h1 class="foo bar">Hello, world!</i></h1>
"#;
let document = Html::parse_document(html);
let selector = Selector::parse("h1.foo bar").unwrap();
let h1 = document.select(&selector).next();
let text = h1.expect("Element was not found").inner_html();
println!("{}", text);
}
It is a simple but not an obvious fix: replace the white space with a dot:
let selector = Selector::parse("h1.foo.bar").unwrap();
Because the classes with spaces denote multiple classes in CSS selectors. Therefore, you need to write the selector to match both classes using the dot denotation.