r - 如何使用R从需要cookie的SSL页面下载压缩文件

标签 r web-scraping rcurl

我正在尝试从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/

相关文章:

R组选择以data.table中的值为条件

python - Selenium webdriver 从 find_elements_by_X 返回空列表

python - 将 URL 中的数据抓取到 pandas 中

javascript - 使用 R 从带有 JavaScript 按钮的 ASP.NET 网页中抓取表格

r - 无法从 github 安装 R-package

r - 按总和聚合数据帧

r - 双和计算,最有效的方法是什么?

r - 计算在多项选择题中选择一个选项同时选择其他每个选项的调查回复的比例

html - 使用 R 提取 html 文本 - 无法访问某些节点

r - devtools::install_github 因 CA 证书错误而失败