html <- minimal_html("
This is a heading
This is a paragraph
This is an important paragraph
")
html %>% html_element("h1")
html %>% html_elements("p")
html %>% html_elements(".important")
html %>% html_elements("#first")
# html_element() vs html_elements() --------------------------------------
html <- minimal_html("
C-3PO is a droid that weighs 167 kg
R2-D2 is a droid that weighs 96 kg
Yoda weighs 66 kg
R4-P17 is a droid
")
li <- html %>% html_elements("li")
# When applied to a node set, html_elements() returns all matching elements
# beneath any of the inputs, flattening results into a new node set.
li %>% html_elements("i")
# When applied to a node set, html_element() always returns a vector the
# same length as the input, using a "missing" element where needed.
li %>% html_element("i")
# and html_text() and html_attr() will return NA
li %>% html_element("i") %>% html_text2()
li %>% html_element("span") %>% html_attr("class")
Run the code above in your browser using DataLab