pub trait SelectorBase {
fn from_html(html: String) -> Self;
fn html(&self) -> String;
fn css(&self, css_selector: &'static str) -> Vec {
let html = nipper::Document::from(self.html().as_str());
let mut output = vec![];
for item in html.select(css_selector).iter() {
output.push(T::from_html(item.html().to_string()))
}
output
}
fn css_once(&self, css_selector: &'static str) -> Option {
self.css(css_selector).pop()
}
fn content(&self) -> String {
let html = nipper::Document::from(self.html().as_str());
html.select("body > *")
.iter()
.map(|element| element.text().to_string())
.last()
.unwrap()
}
fn attr(&self, attribute: &'static str) -> Option {
let html = nipper::Document::from(self.html().as_str());
match html.select("body > *").attr(attribute) {
Some(text) => Some(format!("{}", text)),
None => None,
}
}
}
#[derive(Debug)]
pub struct Selector {
text: String,
}
impl SelectorBase for Selector {
fn from_html(html: String) -> Self {
Selector {
text: html.to_string(),
}
}
fn html(&self) -> String {
format!("{}", self.text)
}
}