我试图从需要我的用户和密码的网站自动下载图片。为了保持我的身份验证,我创建了一个类" CURLHandle"这保留了我的凭据。我无法理解在下载时如何通过cookie传递对象。
#install_github("omegahat/RHTMLForms")
library(RHTMLForms)
library(RCurl)
require(XML)
#create connection function from login form
login<-getHTMLFormDescription("http://mysite//Login.php")
login<-login$Login
submit<-createFunction(login)
#create section with cookiefile
curl = getCurlHandle(cookiefile = "", verbose = TRUE)
#Log in
submit(Password=mypass,User=myuser,.curl = curl )
#now I can navigate on the site
my_page<-getURL("http://mysite/table.php?id=988", curl = curl)
#I get for the id 988 an Url png image
my_picture<-getHTMLExternalFiles(my_page)[1]
my_picture<-paste("http://mysite/",my_picture,sep="")
setwd("c:\\temp")
download.file(my_picture, 'my_pic.png', extra = 'curl')
> trying URL '...'
> Error in download.file(my_picture, "my_pic.png", extra = "curl") :
> cannot open URL '...'
答案 0 :(得分:3)
您可以使用getBinaryURL
:
myBin <- getBinaryURL(my_picture, curl = curl)
writeBin(myBin, "my_pic.png")