【问题标题】:How to use R to download a zipped file from a SSL page that requires cookies如何使用 R 从需要 cookie 的 SSL 页面下载压缩文件
【发布时间】:2012-10-23 15:49:33
【问题描述】:

我正在尝试从 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 的初学者,不知道这个网站是不是很难,或者我只是错过了一些明显的东西。

谢谢!

【问题讨论】:

  • 这个来自 Rhelp 的 url 可能有用:我保存了它但还不需要它:(LINK)

标签: r web-scraping rcurl


【解决方案1】:

使用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")

【讨论】:

  • “raw”参数会导致 content() 中断.. 没有它也可以工作:)
猜你喜欢
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-17
  • 1970-01-01
  • 2011-10-24
  • 2020-06-16
  • 1970-01-01
相关资源
最近更新 更多