gpt4 book ai didi

R httr 认证后下载在交互模式下工作但功能失败

转载 作者:行者123 更新时间:2023-12-04 09:38:04 27 4
gpt4 key购买 nike

下面的代码在交互模式下工作正常,但在函数中使用时失败。非常简单的两个身份验证 POST命令后跟数据下载。我的目标是让它在一个函数中工作,而不仅仅是在交互模式下。

这个问题有点像 this question 的续集.. icpsr 最近更新了他们的网站。下面的最小可重现示例需要一个免费帐户,可在

https://www.icpsr.umich.edu/rpxlogin?path=ICPSR&request_uri=https%3a%2f%2fwww.icpsr.umich.edu%2ficpsrweb%2findex.jsp

我尝试添加 Sys.sleep(1)和各种 httr::GET/httr::POST电话,但没有任何效果。

my_download <-
function( your_email , your_password ){

values <-
list(
agree = "yes",
path = "ICPSR" ,
study = "21600" ,
ds = "" ,
bundle = "rdata",
dups = "yes",
email=your_email,
password=your_password
)


httr::POST("https://www.icpsr.umich.edu/cgi-bin/terms", body = values)
httr::POST("https://www.icpsr.umich.edu/rpxlogin", body = values)

tf <- tempfile()
httr::GET(
"https://www.icpsr.umich.edu/cgi-bin/bob/zipcart2" ,
query = values ,
httr::write_disk( tf , overwrite = TRUE ) ,
httr::progress()
)

}

# fails
my_download( "email@address.com" , "some_password" )

# stepping through works
debug( my_download )
my_download( "email@address.com" , "some_password" )

编辑 失败只是像未登录一样下载此页面(而不是数据集),因此由于某种原因它正在丢失身份验证。如果您登录icpsr,使用隐私浏览查看页面--

https://www.icpsr.umich.edu/cgi-bin/bob/zipcart2?study=21600&ds=1&bundle=rdata&path=ICPSR

谢谢!

最佳答案

此类事情可能发生的原因是状态(例如 cookie)httr包店在handle对于每个 URL(见 ?handle)。

在这种特殊情况下,尚不清楚究竟是什么让它起作用,但一种策略是包含 GET请求到 https://www.icpsr.umich.edu/cgi-bin/bob/在验证和请求数据之前。例如,

my_download <-
function( your_email , your_password ){
## for some reason this is required ...
httr::GET("https://www.icpsr.umich.edu/cgi-bin/bob/")
values <-
list(
agree = "yes",
path = "ICPSR" ,
study = "21600" ,
ds = "" ,
bundle = "rdata",
dups = "yes",
email=your_email,
password=your_password
)
httr::POST("https://www.icpsr.umich.edu/rpxlogin", body = values)
httr::POST("https://www.icpsr.umich.edu/cgi-bin/terms", body = values)
tf <- tempfile()
httr::GET(
"https://www.icpsr.umich.edu/cgi-bin/bob/zipcart2" ,
query = values ,
httr::write_disk( tf , overwrite = TRUE ) ,
httr::progress()
)
}

似乎工作正常,尽管尚不清楚 GET请求到 https://www.icpsr.umich.edu/cgi-bin/bob/ ` 确实或为什么需要它。

关于R httr 认证后下载在交互模式下工作但功能失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48952914/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com