causal-agent / scraper

HTML parsing and querying with CSS selectors
https://docs.rs/scraper
ISC License
1.79k stars 98 forks source link

[Feature Request] Find by Text #160

Closed seongs1024 closed 1 month ago

seongs1024 commented 8 months ago

In the case, select tags which are siblings of a tag containing some text.

I don't have clue to do that... It would be great to have a method, find by name, like BeautifulSoup.

Thank you

seongs1024 commented 8 months ago

To get text of the sibling, this code works but look bad to me

fn find(html: &str, selector: Arc<Selector>, text: &str) -> String {
    let document = Html::parse_fragment(&html);
    document
        .select(&selector)
        .filter(|e| {
            let trimed = e.text().next().map(|e| e.trim());
            trimed == Some(text)
        })
        .flat_map(|e| e.next_sibling_element())
        .map(|e| e.text().collect::<String>())
        .map(|s| s.trim().to_string())
        .collect()
}