html_attr()
gets a single attribute; html_attrs()
gets all attributes.
Arguments
- x
A document (from
read_html()
), node set (fromhtml_elements()
), node (fromhtml_element()
), or session (fromsession()
).- name
Name of attribute to retrieve.
- default
A string used as a default value when the attribute does not exist in every element.
Examples
html <- minimal_html('<ul>
<li><a href="https://a.com" class="important">a</a></li>
<li class="active"><a href="https://c.com">b</a></li>
<li><a href="https://c.com">b</a></li>
</ul>')
html %>% html_elements("a") %>% html_attrs()
#> [[1]]
#> href class
#> "https://a.com" "important"
#>
#> [[2]]
#> href
#> "https://c.com"
#>
#> [[3]]
#> href
#> "https://c.com"
#>
html %>% html_elements("a") %>% html_attr("href")
#> [1] "https://a.com" "https://c.com" "https://c.com"
html %>% html_elements("li") %>% html_attr("class")
#> [1] NA "active" NA
html %>% html_elements("li") %>% html_attr("class", default = "inactive")
#> [1] "inactive" "active" "inactive"