我正在尝试从https页面下载文件,该文件需要按下“我同意”按钮,然后存储cookie。如果这个答案在某处很明显,我深表歉意。
当我直接在Chrome中打开网页并单击“我同意”时-文件开始自动下载。
http://www.icpsr.umich.edu/cgi-bin/bob/zipcart2?path=SAMHDA&study=32722&bundle=delimited&ds=1&dups=yes
我尝试复制this example,但是我不认为hangseng网站实际上存储了cookie/身份验证,因此我不知道该示例是否应该是我所需要的。
除此之外,我认为SSL会使身份验证变得复杂,因为我认为getURL()调用将需要证书规范,例如cainfo = system.file(“CurlSSL”,“cacert.pem”,package =“RCurl”))
我对RCurl的初学者太了解了,无法知道这个网站是否非常困难,或者我是否只是缺少明显的东西。
谢谢!
最佳答案
使用httr
有点容易,因为它设置了所有内容,以便cookie和https无缝工作。
生成Cookie的最简单方法是让网站为您完成操作,方法是手动发布“我同意”表格生成的信息。然后,您再次请求下载实际文件。
library(httr)
terms <- "http://www.icpsr.umich.edu/cgi-bin/terms"
download <- "http://www.icpsr.umich.edu/cgi-bin/bob/zipcart2"
values <- list(agree = "yes", path = "SAMHDA", study = "32722", ds = "",
bundle = "all", dups = "yes")
# Accept the terms on the form,
# generating the appropriate cookies
POST(terms, body = values)
GET(download, query = values)
# Actually download the file (this will take a while)
resp <- GET(download, query = values)
# write the content of the download to a binary file
writeBin(content(resp, "raw"), "c:/temp/thefile.zip")
关于r - 如何使用R从需要cookie的SSL页面下载压缩文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13204503/