utkarshkukreti / select.rs

A Rust library to extract useful data from HTML documents, suitable for web scraping.
MIT License
971 stars 69 forks source link

iterate over attributes? #46

Closed sp3d closed 6 years ago

sp3d commented 6 years ago

Is it possible to access attributes using select.rs without knowing their names beforehand? If not, could an interface for iterating over (attr name, attr value) pairs be added?

utkarshkukreti commented 6 years ago

You're right, it would be nice to have a function Node::attributes() that does this. Until then you can do it in a somewhat hacky way:

let node = document.find(Class("js-gps-track")).next().unwrap();
match node.data() {
    &select::node::Data::Element(_, ref attrs) => {
        for &(ref name, ref value) in attrs {
            println!("{} = {}", name.local, value);
        }
    },
    _ => {}
}
href = http://chat.stackoverflow.com
class = js-gps-track
data-gps-track = site_switcher.click({ item_type:6 })
sp3d commented 6 years ago

As long as it's possible with the current API this isn't a huge deal (though a convenience function would be nice. Thanks for the help!

sp3d commented 6 years ago

Oops, didn't mean to close.

utkarshkukreti commented 6 years ago

You can now do:

for (name, value) in node.attrs() {
   println!("{} = {}", name, value);
}