Skip to contents

Returns the text of the elements matching a selector, and NA when there are none, so that a missing field on one page stays one missing value in a table instead of an error or a vector of length zero.

Usage

html_text_or_na(page, css, collapse = NULL)

Arguments

page

An xml2 document or node.

css

Character. CSS selector.

collapse

Character or NULL. If given, several matches are joined into one string.

Value

A character vector: one element per match, or a single string if collapse is given; NA if nothing matches.

Examples

page <- xml2::read_html("<p class='a'>One</p><p class='a'>Two</p>")
html_text_or_na(page, "p.a")
#> [1] "One" "Two"
html_text_or_na(page, "p.a", collapse = "\n")
#> [1] "One\nTwo"
html_text_or_na(page, "p.b")
#> [1] NA