Learn R Programming

rvest (version 1.0.1)

html_attr: Get element attributes

Description

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

Usage

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

Run this code
# NOT RUN {
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()
cast %>% html_attrs()
cast %>% html_attr("href")

# If needed, use url_absolute() to convert to complete urls
url_absolute(html_attr(cast, "href"), url)
# }

Run the code above in your browser using DataLab