Я пытаюсь использовать 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() для приведенного выше кода?
Заранее спасибо!!
read_html(url(i))
, вы захотите обернуть это в операторе try catch.review <- tryCatch({ read_html(url(i)) }, error = function(e)...)
. Я не на 100% уверен, что это будет работать как задумано в средеparlapply
. - person Chabo   schedule 14.01.2019