r - 如何通过 Rcurl 使用 cookie 下载文件

标签 r cookies rcurl

我正在尝试从需要我的用户名和密码的站点自动下载图片。为了保留我的身份验证,我创建了一个类“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 '...'

最佳答案

您可以使用getBinaryURL:

myBin <- getBinaryURL(my_picture, curl = curl)
writeBin(myBin, "my_pic.png")

关于r - 如何通过 Rcurl 使用 cookie 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29752418/

相关文章:

javascript - 尝试使用 JavaScript 将用户名和密码存储在 cookie 中

r - 将 url 中的 xls 文件下载到数据框(Rcurl)中?

r - 使用 RCurl 登录 mediawiki

r - RMarkdown 和 Beamer 中的增量子项目符号

r - tibble::add_row 到嵌套 tibble 在 tidyr 1.0.0 下抛出错误

cookies - Cookie 的 setMaxAge 的 0 和负值之间的差异

cookies - 神秘的Cookie过期日期 `1969-12-31T23:59:59.000Z`

r - 将 RCurl 与 SFTP 一起使用

r - 当涉及因素时,用向量创建矩阵

r - 在 data.table 中向量化 R for 循环