43 lines
936 B
Rust
43 lines
936 B
Rust
|
pub trait SelectorBase {
|
||
|
fn html(&self) -> &str;
|
||
|
|
||
|
fn css(&self, css_selector: &'static str) -> Vec<Selector> {
|
||
|
let html = nipper::Document::from(self.html());
|
||
|
html.select(css_selector)
|
||
|
.iter()
|
||
|
.map(|element| Selector {
|
||
|
text: element.html().to_string(),
|
||
|
})
|
||
|
.into_iter()
|
||
|
.collect::<Vec<_>>()
|
||
|
}
|
||
|
|
||
|
fn content(&self) -> String {
|
||
|
let html = nipper::Document::from(self.html());
|
||
|
html.select("*")
|
||
|
.iter()
|
||
|
.map(|element| element.text().to_string())
|
||
|
.last()
|
||
|
.unwrap()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
pub struct Selector {
|
||
|
text: String,
|
||
|
}
|
||
|
|
||
|
impl SelectorBase for Selector {
|
||
|
fn html(&self) -> &str {
|
||
|
self.text.as_str()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Selector {
|
||
|
pub fn from_html(html: &'static str) -> Self {
|
||
|
Selector {
|
||
|
text: html.to_string(),
|
||
|
}
|
||
|
}
|
||
|
}
|