Problem with using

Dear rust community,

I'm using to extract some info from a web page

println!("{:?}", document.find(Class("p-org")).next());

output is

Some(Element { name: "span", attrs: [("class", "p-org")], children: [Element { name: "div", attrs: [], children: [Text("University of California, Santa Cruz")] }] })

How do I extract the info of University of California, Santa Cruz


according to the docs it seems you can chain predicates, so try below:

use predicate::Predicate;

let pred = predicate::Class("p-org")
let mut matches = document.find(pred);
let node =
    .expect("Failed to find matching node");
let info = node.as_text()
    .expect("Matching node is not a text node");
println!("{}", info);

It reports the error

error[E0599]: no method named `child` found for type `select::predicate::Class<&str>` in the current scope

You need to use select::predicate::Predicate within its scope to invoke methods defined on this trait. I fixed the example.

It seems like lots of beginners have issues with invoking methods that are imported with a trait. Maybe this section of The Book should be expanded. It’s not the most obvious concept. I definitely remember having trouble with it at first.

1 Like

original example after adding use select::predicate::Predicate;leads me to

error[E0433]: failed to resolve: use of undeclared type or module `predicate`

if I change the example to

let pred = Class("p-org")

It reports

error[E0599]: no associated item named `Name` found for type `dyn select::predicate::Predicate` in the current scope

Why I can't extract the information directly form Some(Element { name: "span", attrs: [("class", "p-org")], children: [Element { name: "div", attrs: [], children: [Text("University of California, Santa Cruz")] }] })

You can, but I thought it's better to build more accurate selector to match the target text node directly.

predicate intended to be select::predicate module. You can either prepend select:: path to them same as you did for predicate::Predicate, or add use select::predicate at the top to import its name into local scope.

1 Like

Thanks, you're right, I get what I need by following your suggestion.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.