Skip to main content

teaql_tool_extra/
html.rs

1use teaql_tool_core::{Result, TeaQLToolError};
2use scraper::{Html, Selector};
3
4#[derive(Debug, Clone)]
5pub struct HtmlTool;
6
7impl HtmlTool {
8    pub fn new() -> Self { Self }
9
10    pub fn select_text(&self, html_str: &str, selector_str: &str) -> Result<Vec<String>> {
11        let document = Html::parse_document(html_str);
12        let selector = Selector::parse(selector_str).map_err(|e| TeaQLToolError::ParseError(format!("{:?}", e)))?;
13        
14        let mut results = Vec::new();
15        for element in document.select(&selector) {
16            results.push(element.text().collect::<Vec<_>>().join(" "));
17        }
18        Ok(results)
19    }
20
21    pub fn select_attr(&self, html_str: &str, selector_str: &str, attr: &str) -> Result<Vec<String>> {
22        let document = Html::parse_document(html_str);
23        let selector = Selector::parse(selector_str).map_err(|e| TeaQLToolError::ParseError(format!("{:?}", e)))?;
24        
25        let mut results = Vec::new();
26        for element in document.select(&selector) {
27            if let Some(val) = element.value().attr(attr) {
28                results.push(val.to_string());
29            }
30        }
31        Ok(results)
32    }
33}
34
35impl Default for HtmlTool {
36    fn default() -> Self { Self::new() }
37}