我正在用 Rselenium 和 phantomjs 创建一个刮板。有时我查询网站的程序耗时太长,而且永远不会结束。所以我正在编写一个超时处理程序。
library(RSelenium)
library(R.utils)
pJS <- phantom(pjs_cmd ="C:\\software\\phantomjs-2.0.0-windows\\bin\\phantomjs.exe" )
UA<-'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0'
eCap <- list(phantomjs.page.settings.userAgent=UA )
remDr <- remoteDriver(browserName = "phantomjs", extraCapabilities = eCap)
remDr$open(silent=T)
time_out<-0
tryCatch({withTimeout({
remDr$navigate("http://stackoverflow.com/questions/14399205/in-r-how-to-make-the-variables-inside-a-function-available-to-the-lower-level-f")
}, envir=globalenv(),timeout=1.08);
}, TimeoutException=function(ex) {
time_out<<-1
})
但我得到错误:Undefined error in RCurl call.Error in queryRD(paste0(serverURL, "/session/", sessionInfo$id, "/url"), :
无论如何,如果我尝试向内看remDr
...
remDr$getTitle()[[1]]
[1] "In R, how to make the variables inside a function available to the lower level function inside this function?(with, attach, environment) - Stack Overflow"
所以它奏效了!但为什么我得到错误?