简体   繁体   English

(...) is not a length 1 atomic vector 错误

[英](...) is not a length 1 atomic vector error

I am trying to scrape IMDB data, and for one variable I keep getting an error.我正在尝试抓取 IMDB 数据,但对于一个变量,我不断收到错误消息。

Error: Result 28 is not a length 1 atomic vector错误:结果 28 不是长度为 1 的原子向量

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当我用 try 做时(如代码中所示),我得到

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> [1] "Error: cannot convert object to a data frame\n" attr(,"class") [1] "try-error" attr(,"condition") <Rcpp::exception: 无法将对象转换为数据框架>

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 .当您遇到NULL时可能会发生这种情况。 Try:尝试:

map_if(is_empty, ~ NA_character_)

in the pipe chain and see if it works.在管链中,看看它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM