I am trying to scrape IMDB data, and for one variable I keep getting an error.
Error: Result 28 is not a length 1 atomic vector
library(rvest)
library(purrr)
library(tidyr)
topmovies <- read_html("http://www.imdb.com/chart/top")
links <- topmovies %>%
html_nodes(".titleColumn") %>%
html_nodes("a") %>%
html_attr("href") %>%
xml2::url_absolute("http://imdb.com")
pages <- links %>% map(read_html)
budget <- try(pages %>%
map_chr(. %>%
html_nodes("#titleDetails .txt-block:nth-child(11)") %>%
html_text() %>%
#gsub("\\D", "", .) %>%
extract_numeric()),silent=TRUE)
When I do it with try (as in the code), I get
budget
[1] "Error: cannot convert object to a data frame\n" attr(,"class") [1] "try-error" attr(,"condition") <Rcpp::exception: cannot convert object to a data frame>
It would be great if anyone could tell me what is going wrong / why try isn't just skipping that result?
This can happen when you encounter NULL
. Try:
map_if(is_empty, ~ NA_character_)
in the pipe chain and see if it works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.