scraper icon indicating copy to clipboard operation
scraper copied to clipboard

Selector for nodes

Open latot opened this issue 1 year ago • 3 comments

Hi, actually the select function returns a ElementRef element, which is usually not an issue if we does not have a way to select a Text node, then I found this: https://stackoverflow.com/a/70757577/4652030

For today docs: https://developer.mozilla.org/en-US/docs/Web/CSS/::target-text https://github.com/w3c/csswg-drafts/issues/2208

Be able to select Nodes and not just Elements could be a break change for the selector function, or the need of a different one.

This has some issues that are WIP, so I open this just to have this clear.

latot avatar Sep 23 '24 16:09 latot

So you're asking for a function that returns a Node instead of an ElementRef. What would be the benefit of this?

LoZack19 avatar Oct 04 '24 20:10 LoZack19

mm, how the actual selector would works, if we select a Text element? Text elements are not ElementRef.

latot avatar Oct 04 '24 21:10 latot

Could you show me the kind of syntax that you would like to achieve?

About your question, I would have to try.

LoZack19 avatar Oct 05 '24 07:10 LoZack19

I am closing this for inactivity. Feel free to add a comment / ping me / reopen if needed

cfvescovo avatar Feb 02 '25 00:02 cfvescovo

Hi, sorry for being late, here is an example:

fn main() {
        use scraper::{Html, Selector};

        let html = r#"
            hi
            <p>hi2</p>
        "#;

        let document = Html::parse_document(html);
        let selector = Selector::parse("::target-text").unwrap();

        for element in document.select(&selector) {
                println!("{}", element.value().name());
        }
}

Following the docs, I think this should return two nodes, hi, hi2.

Seems the selector do not support the selector.

Thx!

latot avatar Feb 03 '25 14:02 latot