causal-agent / scraper

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

Is there a way to select element with a node? #104

Closed Kayxue closed 11 months ago

Kayxue commented 1 year ago

I want to select an element from a child node, what should I do? Seems that this way doesn't work. image

j-mendez commented 1 year ago

@Kayxue hi, if you use a match statement here could help.

teymour-aldridge commented 1 year ago

What error message do you get?

iarna commented 11 months ago

children() returns an iterator that produces NodeRefs -- if the child represents an element (eg if child.value().is_element() is true) then you can covert it into an ElementRef with ElementRef::wrap(child) (which returns Option<ElementRef>). If you just needed an Element then child.value().as_element() will get you there.

(Commenting because I was struggling with this, but have since figured it out through source reading.)

teymour-aldridge commented 11 months ago

Amazing, thank you @iarna!