How to save result as "ND" when there is no record? rvest and R
We could check the length
and if it is 0 (length(character(0))
is 0
), change the value to 'ND'
for (i in seq_len(nrow(x))){
html<-x$page[i]%>%
unclass() %>%
unlist()
read_html(html,encoding = "ISO-8859-1") %>%
rvest::html_elements(xpath = '//*[@id="principal"]/table[2]') %>%
rvest::html_elements(xpath = '//div[@id="tituloContext"]') %>%
html_text()%>%
str_replace_all(.,"[\\n\\r\\t]+", "")%>%
stringr::str_trim( ) -> tmp
if(length(tmp) == 0) tmp <- "ND"
x$title[i] <- tmp
}
-checking
> x$title
[1] "CARRINHO DE LIXO PARA LIMPEZA URBANA" "ND"