raspa/src/selector.rs

55 lines
1.3 KiB
Rust
Raw Normal View History

2021-01-18 02:01:06 +00:00
pub trait SelectorBase {
fn from_html(html: String) -> Self;
2021-01-18 02:01:06 +00:00
fn html(&self) -> String;
fn css<T: SelectorBase>(&self, css_selector: &'static str) -> Vec<T> {
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<T: SelectorBase>(&self, css_selector: &'static str) -> Option<T> {
self.css(css_selector).pop()
2021-01-18 02:01:06 +00:00
}
fn content(&self) -> String {
let html = nipper::Document::from(self.html().as_str());
2021-01-31 03:11:54 +00:00
html.select("body > *")
2021-01-18 02:01:06 +00:00
.iter()
.map(|element| element.text().to_string())
.last()
.unwrap()
}
2021-01-31 03:11:54 +00:00
fn attr(&self, attribute: &'static str) -> Option<String> {
let html = nipper::Document::from(self.html().as_str());
match html.select("body > *").attr(attribute) {
Some(text) => Some(format!("{}", text)),
None => None,
}
}
2021-01-18 02:01:06 +00:00
}
#[derive(Debug)]
pub struct Selector {
text: String,
}
impl SelectorBase for Selector {
fn from_html(html: String) -> Self {
2021-01-18 02:01:06 +00:00
Selector {
text: html.to_string(),
}
}
fn html(&self) -> String {
format!("{}", self.text)
}
2021-01-18 02:01:06 +00:00
}