Как использовать trycatch () для parLapply для ошибки: не удается открыть соединение? - PullRequest
0 голосов
/ 14 января 2019

Я пытаюсь использовать parlapply для очистки данных от нескольких URL. Я получаю эту ошибку

sites <- c("https://forums.vwvortex.com/showthread.php?5494121-euro-spec-parts", "https://forums.vwvortex.com/showthread.php?5489376-Is-this-normal", "https://forums.vwvortex.com/showthread.php?5490376-rear-hatch-light")

У меня есть около 5000 URL для просмотра.

nCores <- detectCores(logical = FALSE)
cat(nCores, " cores detected.")

# detect threads with parallel()
nThreads<- detectCores(logical = TRUE)
cat(nThreads, " threads detected.")

# Create doSNOW compute cluster (try 64)
# One can increase up to 128 nodes
# Each node requires 44 Mbyte RAM under WINDOWS.
cluster <- makeCluster(nThreads, type = "SOCK")
class(cluster);

# register the cluster
registerDoSNOW(cluster)

#get info
getDoParWorkers(); getDoParName();

strt <- Sys.time()

results <- parLapply(cluster, sites, function(i) {
  library(dplyr)
  library(xml2)
  library(magrittr)
  library(rvest)
  review <- read_html(url(i))  
  threads<- cbind(review %>% html_nodes("blockquote.postcontent.restore") %>% html_text())
  datethreads <- cbind(review %>% html_nodes("span.date") %>% html_text())
  userinfo <- cbind(review %>% html_nodes("div.username_container") %>% html_text())
  title <- cbind(review %>% html_nodes("li.navbit.lastnavbit") %>%  html_text())
  urls <- cbind(review %>% html_nodes("span.threadtitle") %>% html_nodes("a") %>%   html_attr("href") %>%  paste0("https://forums.vwvortex.com/", .) )
  links <- sub("&.*","", urls)
  library(rowr)
  x <- data.frame(rowr::cbind.fill(threads, datethreads, userinfo, title, links, fill = NA), stringsAsFactors = FALSE)
  return(x)

}) 
print(Sys.time()-strt)

Error in checkForRemoteErrors(val) : 
  one node produced an error: cannot open the connection 

Так как ошибка неопределенная. Я хочу использовать trycatch () для обработки ошибок.

Я не уверен, но я чувствую, что ошибка, должно быть, была в read_html (url (i)), я попробовал следующий код:

results <- parLapply(cluster, sites, function(i) {
  library(dplyr)
  library(xml2)
  library(magrittr)
  library(rvest)
  tryCatch(
  review <- read_html(url(i)),
error = function(e) {

message("Here's the original error message:")
message(e)
return(NULL)  
})
  threads<- cbind(review %>% html_nodes("blockquote.postcontent.restore") %>% html_text())
  datethreads <- cbind(review %>% html_nodes("span.date") %>% html_text())
  userinfo <- cbind(review %>% html_nodes("div.username_container") %>% html_text())
  title <- cbind(review %>% html_nodes("li.navbit.lastnavbit") %>%  html_text())
  urls <- cbind(review %>% html_nodes("span.threadtitle") %>% html_nodes("a") %>%   html_attr("href") %>%  paste0("https://forums.vwvortex.com/", .) )
  links <- sub("&.*","", urls)
  library(rowr)
  x <- data.frame(rowr::cbind.fill(threads, datethreads, userinfo, title, links, fill = NA), stringsAsFactors = FALSE)
  return(x)

}) 

Не уверен, как это сделать. Любая помощь с тем, что, возможно, вызвало не может открыть ошибку соединения или как написать trycatch () для приведенного выше кода?

Спасибо заранее !!

...