Learn R Programming

rvest (version 1.0.4)

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)

Value

A character vector (for html_attr()) or list (html_attrs()) the same length as 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.

Examples

Run this code
html <- minimal_html('
  a
  b
  b
  ')

html %>% html_elements("a") %>% html_attrs()

html %>% html_elements("a") %>% html_attr("href")
html %>% html_elements("li") %>% html_attr("class")
html %>% html_elements("li") %>% html_attr("class", default = "inactive")

Run the code above in your browser using DataLab