私はこれを十分に長い間困惑させてきましたが、それを回避する方法を理解できないようです. 動作するダミーコードを与えるのが最も簡単です:
require(RCurl)
require(XML)
#set a bunch of options for curl
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
agent="Firefox/23.0"
curl = getCurlHandle()
curlSetOpt(
cookiejar = 'cookies.txt' ,
useragent = agent,
followlocation = TRUE ,
autoreferer = TRUE ,
httpauth = 1L, # "basic" http authorization version -- this seems to make a difference for India servers
curl = curl
)
list1 <- c('http://timesofindia.indiatimes.com//articleshow/2933112.cms',
'http://timesofindia.indiatimes.com//articleshow/2933131.cms',
'http://timesofindia.indiatimes.com//articleshow/2933209.cms',
'http://timesofindia.indiatimes.com//articleshow/2933277.cms')
#note list2 has a new link inserted in 2nd position; this is the link that kills the following getURL calls
list2 <- c('http://timesofindia.indiatimes.com//articleshow/2933112.cms',
'http://timesofindia.indiatimes.com//articleshow/2933019.cms',
'http://timesofindia.indiatimes.com//articleshow/2933131.cms',
'http://timesofindia.indiatimes.com//articleshow/2933209.cms',
'http://timesofindia.indiatimes.com//articleshow/2933277.cms')
for ( i in seq( list1 ) ){
print(list1[i])
html <-
try( getURL(
list1[i],
maxredirs = as.integer(20),
followlocation = TRUE,
curl = curl
),TRUE)
if (class (html) == "try-error") {
print(paste("error accessing",list1[i]))
rm(html)
gc()
next
} else {
print('success')
}
}
gc()
for ( i in seq( list2 ) ){
print(list2[i])
html <-
try( getURL(
list2[i],
maxredirs = as.integer(20),
followlocation = TRUE,
curl = curl
),TRUE)
if (class (html) == "try-error") {
print(paste("error accessing",list2[i]))
rm(html)
gc()
next
} else {
print('success')
}
}
これは、RCurl および XML ライブラリがインストールされた状態で実行できるはずです。ポイントhttp://timesofindia.indiatimes.com//articleshow/2933019.cms
は、リストの 2 番目の位置に挿入すると、残りのループの成功が失われることです (他のリンクも同じです)。これは、リンクに PDF が含まれている場合に (この状況でも他の状況でも一貫して) 発生します (確認してください)。
これを修正して、PDF を含むリンクを取得してもループが停止しないようにする方法について何か考えはありますか? ご覧のとおり、問題のある可能性のあるオブジェクトをgc()
いたるところにクリアしようとしましたが、PDF がループを強制終了する理由がわかりません。
ありがとう!
確認のために、2 つのfor
ループの出力を次に示します。
#[1] "http://timesofindia.indiatimes.com//articleshow/2933112.cms"
#[1] "success"
#[1] "http://timesofindia.indiatimes.com//articleshow/2933131.cms"
#[1] "success"
#[1] "http://timesofindia.indiatimes.com//articleshow/2933209.cms"
#[1] "success"
#[1] "http://timesofindia.indiatimes.com//articleshow/2933277.cms"
#[1] "success"
と
#[1] "http://timesofindia.indiatimes.com//articleshow/2933112.cms"
#[1] "success"
#[1] "http://timesofindia.indiatimes.com//articleshow/2933019.cms"
#[1] "error accessing http://timesofindia.indiatimes.com//articleshow/2933019.cms"
#[1] "http://timesofindia.indiatimes.com//articleshow/2933131.cms"
#[1] "error accessing http://timesofindia.indiatimes.com//articleshow/2933131.cms"
#[1] "http://timesofindia.indiatimes.com//articleshow/2933209.cms"
#[1] "error accessing http://timesofindia.indiatimes.com//articleshow/2933209.cms"
#[1] "http://timesofindia.indiatimes.com//articleshow/2933277.cms"
#[1] "error accessing http://timesofindia.indiatimes.com//articleshow/2933277.cms"