我相信获得有效的cookie的唯一方法是使用库(curlconverter)(参见How to download a file behind a semi-broken javascript asp function with R),但该回答似乎不足以以编程方式确定文件的http url,只有在已知的时候下载压缩文件.
我已经粘贴了一些代码,使用不同的httr和curlconverter代码,我已经玩了,但我在这里遗漏了一些东西.再次,唯一的目标是以编程方式在R(跨平台)内完全确定突出显示的文本.
library(curlconverter) library(httr) browserPOST <- "curl 'http://www.worldvaluessurvey.org/AJDownload.jsp' -H 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Accept-Encoding:gzip,deflate' -H 'Accept-Language:en-US,en;q=0.8' -H 'Cache-Control:max-age=0' --compressed -H 'Connection:keep-alive' -H 'Content-Length:188' -H 'Content-Type:application/x-www-form-urlencoded' -H 'Cookie:ASPSESSIONIDCASQAACD=IBLGBFOAEHFILMMJJCFEOEMI; JSESSIONID=50DABDEDD0B2FC370C415B4BD1855260; __atuvc=13%7C45; __atuvs=58224f37d312c42400c' -H 'Host:www.worldvaluessurvey.org' -H 'Origin:http://www.worldvaluessurvey.org' -H 'Referer:http://www.worldvaluessurvey.org/AJDownloadLicense.jsp' -H 'Upgrade-Insecure-Requests:1' -H 'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/54.0.2840.71 Safari/537.36'" form_data <- list( ulthost = "WVS",CMSID = "",LITITLE = "",LINOMBRE = "fas",LIEMPRESA = "asf",LIEMAIL = "asdf",LIPROJECT = "asfd",LIUSE = "1",LIPURPOSE = "asdf",LIAGREE = "1",DOID = "3996",CndWAVE = "-1",SAID = "-1",AJArchive = "WVS Data Archive",EdFunction = "",DOP = "" ) getDATA <- (straighten(browserPOST) %>% make_req)[[1]]() a <- VERB(verb = "POST",url = "http://www.worldvaluessurvey.org/AJDownload.jsp",httr::add_headers(Accept = "text/html,*/*;q=0.8",`Accept-Encoding` = "gzip,deflate",`Accept-Language` = "en-US,en;q=0.8",`Cache-Control` = "max-age=0",Connection = "keep-alive",`Content-Length` = "188",Host = "www.worldvaluessurvey.org",Origin = "http://www.worldvaluessurvey.org",Referer = "http://www.worldvaluessurvey.org/AJDownloadLicense.jsp",`Upgrade-Insecure-Requests` = "1",`User-Agent` = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/54.0.2840.71 Safari/537.36"),httr::set_cookies(`Cookie:ASPSESSIONIDCASQAACD` = "IBLGBFOAEHFILMMJJCFEOEMI",JSESSIONID = "50DABDEDD0B2FC370C415B4BD1855260",`__atuvc` = "13%7C45",`__atuvs` = "58224f37d312c42400c"),encode = "form",body=form_data)
解决方法
问题与R语言无关.如果我们尝试将一些数据发布到下载脚本,我们将以任何语言获得相同的结果.我们必须在这里处理某种安全的“模式”.该网站限制用户检索文件URL,并且要求他们填写表单以提供数据,以便提供这些链接.如果浏览器可以检索这些链接,那么我们也可以通过编写适当的HTTP调用.事实上,我们需要知道我们必须做出哪些呼叫.为了找到这一点,我们需要看到网站在有人点击下载时进行的个人呼叫.这是我在一个成功的302 AJDownload.jsp POST调用之前发现几个电话:
我们可以清楚地看到它,如果我们看AJDocumentation.jsp的源码,它通过使用jQuery $.get来进行这些调用:
$.get("http://ipinfo.io?token=xxxxxxxxxxxxxx",function (response) { var geodatos=encodeURIComponent(response.ip+"\t"+response.country+"\t"+response.postal+"\t"+ response.loc+"\t"+response.region+"\t"+response.city+"\t"+ response.org); $.get("jdsStatJD.jsp?ID="+geodatos+ "&url=http%3A%2F%2Fwww.worldvaluessurvey.org%2FAJDocumentation.jsp&referer=null&cms=Documentation",function (resp2) { }); },"jsonp");
然后,下面几个电话,我们可以看到成功的POST /AJDownload.jsp,状态为302移动临时和在其响应头中的想要的位置:
HTTP/1.1 302 Moved Temporarily Content-Length: 0 Content-Type: text/html Location: http://www.worldvaluessurvey.org/wvsdc/CO00001/F00003724-WVS_Longitudinal_1981-2014_stata_dta_v_2015_04_18.zip Server: Microsoft-IIS/7.5 X-Powered-By: ASP.NET Date: Thu,01 Dec 2016 16:24:37 GMT
所以这是这个网站的安全机制.在用户即将通过点击链接开始下载之前,它使用ipinfo.io存储有关其IP,位置甚至ISP组织的访问者信息.接收这些数据的脚本是/jdsStatJD.jsp.我没有使用ipinfo.io,也没有使用这个服务的API密钥(隐藏在我的屏幕截图上),而是创建了一个虚拟的有效数据序列,只是为了验证请求. “受保护”文件的表单数据根本不需要.可以下载文件而不发布这些数据.
此外,curlconverter库不是必需的.所有我们要做的,是使用httr库简单的GET和POST请求.我想指出的一个重要部分是,为了阻止httr POST功能跟随在上次调用时接收到302状态的Location头,我们需要使用config setting config(followlocation = FALSE),这当然会阻止它从跟随位置,让我们从标题获取位置.
OUTPUT
我的R脚本可以从命令行运行,它可以接受参数的DOID数值来获取所需的文件.例如,如果要获取文件WVS_Longitudinal_1981-2014_stata_dta_v_2015_04_18的链接,那么在使用Rscript命令调用它时,我们必须将其DOID(这是3724)添加到脚本的末尾:
Rscript wvs_fetch_downloads.r 3724 [1] "http://www.worldvaluessurvey.org/wvsdc/CO00001/F00003724-WVS_Longitudinal_1981-2014_stata_dta_v_2015_04_18.zip"
我已经创建了一个R函数来通过传递DOID来获取所需的每个文件位置:
getFileById <- function(fileId)
您可以通过直接传递DOID来删除命令行参数解析并使用该函数:
#args <- commandArgs(TRUE) #if(length(args) == 0) { # print("No file id specified. Use './script.r ####'.") # quit("no") #} #fileId <- args[1] fileId <- "3724" # DOID=3843 : WVS_EVS_Integrated_Dictionary_Codebook v_2014_09_22 (Excel) # DOID=3844 : WVS_Values Surveys Integrated Dictionary_TimeSeries_v_2014-04-25 (Excel) # DOID=3725 : WVS_Longitudinal_1981-2014_rdata_v_2015_04_18 # DOID=3996 : WVS_Longitudinal_1981-2014_sas_v_2015_04_18 # DOID=3723 : WVS_Longitudinal_1981-2014_spss_v_2015_04_18 # DOID=3724 : WVS_Longitudinal_1981-2014_stata_dta_v_2015_04_18 getFileById(fileId)
最终R工作脚本
library(httr) getFileById <- function(fileId) { response <- GET( url = "http://www.worldvaluessurvey.org/AJDocumentation.jsp?CndWAVE=-1",add_headers( `Accept` = "text/html,`Connection` = "keep-alive",`Host` = "www.worldvaluessurvey.org",`User-Agent` = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0",`Content-type` = "application/x-www-form-urlencoded",`Referer` = "http://www.worldvaluessurvey.org/AJDownloadLicense.jsp",`Upgrade-Insecure-Requests` = "1")) set_cookie <- headers(response)$`set-cookie` cookies <- strsplit(set_cookie,';') cookie <- cookies[[1]][1] response <- GET( url = "http://www.worldvaluessurvey.org/jdsStatJD.jsp?ID=2.72.48.149%09IT%09undefined%0941.8902%2C12.4923%09Lazio%09Roma%09Orange%20SA%20Telecommunications%20Corporation&url=http%3A%2F%2Fwww.worldvaluessurvey.org%2FAJDocumentation.jsp&referer=null&cms=Documentation",add_headers( `Accept` = "*/*",`X-Requested-With` = "XMLHttpRequest",`Referer` = "http://www.worldvaluessurvey.org/AJDocumentation.jsp?CndWAVE=-1",`Cookie` = cookie)) post_data <- list( ulthost = "WVS",DOID = fileId,DOP = "",PUB = "") response <- POST( url = "http://www.worldvaluessurvey.org/AJDownload.jsp",config(followlocation = FALSE),`Cookie` = cookie),body = post_data,encode = "form") location <- headers(response)$location location } args <- commandArgs(TRUE) if(length(args) == 0) { print("No file id specified. Use './script.r ####'.") quit("no") } fileId <- args[1] # DOID=3843 : WVS_EVS_Integrated_Dictionary_Codebook v_2014_09_22 (Excel) # DOID=3844 : WVS_Values Surveys Integrated Dictionary_TimeSeries_v_2014-04-25 (Excel) # DOID=3725 : WVS_Longitudinal_1981-2014_rdata_v_2015_04_18 # DOID=3996 : WVS_Longitudinal_1981-2014_sas_v_2015_04_18 # DOID=3723 : WVS_Longitudinal_1981-2014_spss_v_2015_04_18 # DOID=3724 : WVS_Longitudinal_1981-2014_stata_dta_v_2015_04_18 getFileById(fileId)