Linux   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了以编程方式刮取R中的响应标头大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在尝试访问突出显示的响应标题:下面的屏幕截图中的位置文本仅使用R及其基于卷曲的webscraping库.您可以在任何网页浏览器中轻松访问这一点,方法是访问 http://www.worldvaluessurvey.org/WVSDocumentationWVl.jsp,点击任何数据文件的下载,并填写协议表单.下载在网络浏览器中自动开始. 我相信获得有效的cookie的唯一方法是使用库(curl
我正在尝试访问突出显示的响应标题:下面的屏幕截图中的位置文本仅使用R及其基于卷曲的webscraping库.您可以在任何网页浏览器中轻松访问这一点,方法是访问 http://www.worldvaluessurvey.org/WVSDocumentationWVL.jsp,点击任何数据文件的下载,并填写协议表单.下载在网络浏览器中自动开始.

我相信获得有效的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)

大佬总结

以上是大佬教程为你收集整理的以编程方式刮取R中的响应标头全部内容,希望文章能够帮你解决以编程方式刮取R中的响应标头所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。