html_attr() gets a single attribute; html_attr() gets all attributes.

html_attr(x, name, default = NA_character_)

html_attrs(x)

Arguments

x

A document (from read_html()), node set (from html_elements()), node (from html_element()), or session (from session()).

name

Name of attribute to retrieve.

default

A string used as a default value when the attribute does not exist in every element.

Value

A character vector (for html_attr()) or list (html_attrs()) the same length as x.

Examples

url <- "https://en.wikipedia.org/w/index.php?title=The_Lego_Movie&oldid=998422565" html <- read_html(url) cast <- html_elements(html, "tr:nth-child(8) .plainlist a") cast %>% html_text2()
#> [1] "Chris Pratt" "Will Ferrell" "Elizabeth Banks" "Will Arnett" #> [5] "Nick Offerman" "Alison Brie" "Charlie Day" "Liam Neeson" #> [9] "Morgan Freeman"
cast %>% html_attrs()
#> [[1]] #> href title #> "/wiki/Chris_Pratt" "Chris Pratt" #> #> [[2]] #> href title #> "/wiki/Will_Ferrell" "Will Ferrell" #> #> [[3]] #> href title #> "/wiki/Elizabeth_Banks" "Elizabeth Banks" #> #> [[4]] #> href title #> "/wiki/Will_Arnett" "Will Arnett" #> #> [[5]] #> href title #> "/wiki/Nick_Offerman" "Nick Offerman" #> #> [[6]] #> href title #> "/wiki/Alison_Brie" "Alison Brie" #> #> [[7]] #> href title #> "/wiki/Charlie_Day" "Charlie Day" #> #> [[8]] #> href title #> "/wiki/Liam_Neeson" "Liam Neeson" #> #> [[9]] #> href title #> "/wiki/Morgan_Freeman" "Morgan Freeman" #>
cast %>% html_attr("href")
#> [1] "/wiki/Chris_Pratt" "/wiki/Will_Ferrell" "/wiki/Elizabeth_Banks" #> [4] "/wiki/Will_Arnett" "/wiki/Nick_Offerman" "/wiki/Alison_Brie" #> [7] "/wiki/Charlie_Day" "/wiki/Liam_Neeson" "/wiki/Morgan_Freeman"
# If needed, use url_absolute() to convert to complete urls url_absolute(html_attr(cast, "href"), url)
#> [1] "https://en.wikipedia.org/wiki/Chris_Pratt" #> [2] "https://en.wikipedia.org/wiki/Will_Ferrell" #> [3] "https://en.wikipedia.org/wiki/Elizabeth_Banks" #> [4] "https://en.wikipedia.org/wiki/Will_Arnett" #> [5] "https://en.wikipedia.org/wiki/Nick_Offerman" #> [6] "https://en.wikipedia.org/wiki/Alison_Brie" #> [7] "https://en.wikipedia.org/wiki/Charlie_Day" #> [8] "https://en.wikipedia.org/wiki/Liam_Neeson" #> [9] "https://en.wikipedia.org/wiki/Morgan_Freeman"