2012-11-02 56 views
6

我試圖從需要「我同意」按鈕的https頁面下載文件,然後存儲cookie。我很抱歉,如果這個答案是顯而易見的地方..如何使用R從需要cookie的SSL頁面下載壓縮文件

當我在Chrome中直接打開網頁,然後單擊「我同意」 - 文件開始自動下載。

http://www.icpsr.umich.edu/cgi-bin/bob/zipcart2?path=SAMHDA&study=32722&bundle=delimited&ds=1&dups=yes

我試圖複製this example,但我不認爲恆生網站實際存儲的Cookie /認證,所以我不知道這是否例子應該是所有我需要的。因爲我認爲getURL()調用將需要像cainfo = system.file(「CurlSSL」,「cacert.pem」,package =「RCurl」)的證書規範, ))

我太多的RCurl的初學者知道這個網站是非常困難的,或者如果我只是錯過了一些明顯的東西。

謝謝!

+0

這從Rhelp網址可能是有用的:我救了它,但沒有需要它:(LINK)(http://r.789695.n4.nabble.com/How-to-set -cookies-in-RCurl-td4632693.html) –

回答

12

這與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") 
+0

「原始」參數導致content()中斷..沒有它的作品:) –

+0

我認爲這意味着您需要更新您的httr – hadley

+0

yup。 update.packages('httr')做到了:) –

相關問題