James-LG / Skyscraper

Rust library for scraping HTML using XPath expressions
MIT License
30 stars 3 forks source link

Get Node attributes method #6

Closed dyens closed 2 years ago

dyens commented 2 years ago

Now for take access of attributes i write:

    let results = expr.apply(&document)?;
    let result = results[0];

    let tag_node = match document.get_html_node(&result).unwrap() {
        HtmlNode::Tag(tag_node) => Some(tag_node),
        _ => None,
    };
    let some_attr = &tag_node.unwrap().attributes["attr_name"];

What do you think, maybe is a good to have some special method for this? Something like this:

// Return attributes if `HtmlNode` is `Tag` else `None`;
result.get_attributes(&document) -> Optional<HashMap<String, String>>

// Or create empty hashmap if not attributes
result.get_attributes(&document) -> HashMap<String, String>

I can create pr for this.

James-LG commented 2 years ago

Yeah if you make it similar to the get_text(&document) functions I would accept it.