以编程方式刮取R中的响应标头

前端之家收集整理的这篇文章主要介绍了以编程方式刮取R中的响应标头前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试访问突出显示的响应标题:下面的屏幕截图中的位置文本仅使用R及其基于卷曲的webscraping库.您可以在任何网页浏览器中轻松访问这一点,方法是访问 @L_403_0@,点击任何数据文件的下载,并填写协议表单.下载在网络浏览器中自动开始.

我相信获得有效的cookie的唯一方法是使用库(curlconverter)(参见How to download a file behind a semi-broken javascript asp function with R),但该回答似乎不足以以编程方式确定文件的http url,只有在已知的时候下载压缩文件.

我已经粘贴了一些代码,使用不同的httr和curlconverter代码,我已经玩了,但我在这里遗漏了一些东西.再次,唯一的目标是以编程方式在R(跨平台)内完全确定突出显示的文本.

  1. library(curlconverter)
  2. library(httr)
  3.  
  4. browserPOST <-
  5. "curl 'http://www.worldvaluessurvey.org/AJDownload.jsp'
  6. -H 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
  7. -H 'Accept-Encoding:gzip,deflate'
  8. -H 'Accept-Language:en-US,en;q=0.8'
  9. -H 'Cache-Control:max-age=0'
  10. --compressed -H 'Connection:keep-alive'
  11. -H 'Content-Length:188'
  12. -H 'Content-Type:application/x-www-form-urlencoded'
  13. -H 'Cookie:ASPSESSIONIDCASQAACD=IBLGBFOAEHFILMMJJCFEOEMI; JSESSIONID=50DABDEDD0B2FC370C415B4BD1855260; __atuvc=13%7C45; __atuvs=58224f37d312c42400c'
  14. -H 'Host:www.worldvaluessurvey.org'
  15. -H 'Origin:http://www.worldvaluessurvey.org'
  16. -H 'Referer:http://www.worldvaluessurvey.org/AJDownloadLicense.jsp'
  17. -H 'Upgrade-Insecure-Requests:1'
  18. -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'"
  19.  
  20. form_data <-
  21. list(
  22. 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 = ""
  23. )
  24.  
  25.  
  26.  
  27. getDATA <- (straighten(browserPOST) %>% make_req)[[1]]()
  28.  
  29. 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来进行这些调用

  1. $.get("http://ipinfo.io?token=xxxxxxxxxxxxxx",function (response) {
  2. var geodatos=encodeURIComponent(response.ip+"\t"+response.country+"\t"+response.postal+"\t"+
  3. response.loc+"\t"+response.region+"\t"+response.city+"\t"+
  4. response.org);
  5.  
  6. $.get("jdsStatJD.jsp?ID="+geodatos+
  7. "&url=http%3A%2F%2Fwww.worldvaluessurvey.org%2FAJDocumentation.jsp&referer=null&cms=Documentation",function (resp2) {
  8. });
  9. },"jsonp");

然后,下面几个电话,我们可以看到成功的POST /AJDownload.jsp,状态为302移动临时和在其响应头中的想要的位置:

  1. HTTP/1.1 302 Moved Temporarily
  2. Content-Length: 0
  3. Content-Type: text/html
  4. Location: http://www.worldvaluessurvey.org/wvsdc/CO00001/F00003724-WVS_Longitudinal_1981-2014_stata_dta_v_2015_04_18.zip
  5. Server: Microsoft-IIS/7.5
  6. X-Powered-By: ASP.NET
  7. 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)添加到脚本的末尾:

  1. Rscript wvs_fetch_downloads.r 3724
  2. [1] "http://www.worldvaluessurvey.org/wvsdc/CO00001/F00003724-WVS_Longitudinal_1981-2014_stata_dta_v_2015_04_18.zip"

我已经创建了一个R函数来通过传递DOID来获取所需的每个文件位置:

  1. getFileById <- function(fileId)

您可以通过直接传递DOID来删除命令行参数解析并使用该函数

  1. #args <- commandArgs(TRUE)
  2. #if(length(args) == 0) {
  3. # print("No file id specified. Use './script.r ####'.")
  4. # quit("no")
  5. #}
  6.  
  7. #fileId <- args[1]
  8. fileId <- "3724"
  9.  
  10. # DOID=3843 : WVS_EVS_Integrated_Dictionary_Codebook v_2014_09_22 (Excel)
  11. # DOID=3844 : WVS_Values Surveys Integrated Dictionary_TimeSeries_v_2014-04-25 (Excel)
  12. # DOID=3725 : WVS_Longitudinal_1981-2014_rdata_v_2015_04_18
  13. # DOID=3996 : WVS_Longitudinal_1981-2014_sas_v_2015_04_18
  14. # DOID=3723 : WVS_Longitudinal_1981-2014_spss_v_2015_04_18
  15. # DOID=3724 : WVS_Longitudinal_1981-2014_stata_dta_v_2015_04_18
  16.  
  17. getFileById(fileId)

最终R工作脚本

  1. library(httr)
  2.  
  3. getFileById <- function(fileId) {
  4. response <- GET(
  5. url = "http://www.worldvaluessurvey.org/AJDocumentation.jsp?CndWAVE=-1",add_headers(
  6. `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"))
  7.  
  8. set_cookie <- headers(response)$`set-cookie`
  9. cookies <- strsplit(set_cookie,';')
  10. cookie <- cookies[[1]][1]
  11.  
  12. response <- GET(
  13. 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(
  14. `Accept` = "*/*",`X-Requested-With` = "XMLHttpRequest",`Referer` = "http://www.worldvaluessurvey.org/AJDocumentation.jsp?CndWAVE=-1",`Cookie` = cookie))
  15.  
  16. post_data <- list(
  17. ulthost = "WVS",DOID = fileId,DOP = "",PUB = "")
  18.  
  19. response <- POST(
  20. url = "http://www.worldvaluessurvey.org/AJDownload.jsp",config(followlocation = FALSE),`Cookie` = cookie),body = post_data,encode = "form")
  21.  
  22. location <- headers(response)$location
  23. location
  24. }
  25.  
  26. args <- commandArgs(TRUE)
  27. if(length(args) == 0) {
  28. print("No file id specified. Use './script.r ####'.")
  29. quit("no")
  30. }
  31.  
  32. fileId <- args[1]
  33.  
  34. # DOID=3843 : WVS_EVS_Integrated_Dictionary_Codebook v_2014_09_22 (Excel)
  35. # DOID=3844 : WVS_Values Surveys Integrated Dictionary_TimeSeries_v_2014-04-25 (Excel)
  36. # DOID=3725 : WVS_Longitudinal_1981-2014_rdata_v_2015_04_18
  37. # DOID=3996 : WVS_Longitudinal_1981-2014_sas_v_2015_04_18
  38. # DOID=3723 : WVS_Longitudinal_1981-2014_spss_v_2015_04_18
  39. # DOID=3724 : WVS_Longitudinal_1981-2014_stata_dta_v_2015_04_18
  40.  
  41. getFileById(fileId)

猜你在找的Linux相关文章