httr & use_proxy 与 RCurl 行为差异

httr & use_proxy vs RCurl behaviour difference

提问人:anonR 提问时间:4/18/2023 最后编辑:IlyaanonR 更新时间:4/29/2023 访问量:119

问:

我正在尝试使用代理服务器,在调用中使用该函数,代理服务器在每次调用时都会设置轮换 IP,但 IP 坚持使用 GET &,而它与系统命令配合正常。 我无法理解为什么两人的行为不同。use_proxyGETuse_proxyRCurlcurl

> ## usname <- "username"
> ## uspwd <- "password"
> 
> ## Ip Sticking
> library(httr)
> library(jsonlite)
> for(idx in 1:3)
+ {
+   message(fromJSON(content(GET("https://ipinfo.io",
+       use_proxy(
+           url = "gate.smartproxy.com",
+           port = 7000,
+           username = usname,
+           password = uspwd
+       )),"text"))$ip)
+ }

output : 
120.188.79.85
120.188.79.85
120.188.79.85
> 
> 
> 
> ## Ip Rotating
> library(RCurl)
> opts <- list(
+   proxy         = "gate.smartproxy.com",
+   proxyusername = usname, 
+   proxypassword = uspwd, 
+   proxyport     = 7000
+ )
> options(RCurlOptions = opts)
> for(idx in 1:3) message(fromJSON(getURL("https://ipinfo.io"))$ip)

output :
42.118.70.31
106.194.152.66
180.183.135.201
r curl httr rcurl

评论


答:

1赞 anonR 4/29/2023 #1

解决方案是在 GET 命令中使用该参数,因此代码应该是,这将防止 httr 重用连接,从而在每次调用时获得一个新 IPconfig(forbid_reuse = TRUE)

library(httr)
library(jsonlite)
for(idx in 1:3)
{
    message(fromJSON(content(GET("https://ipinfo.io",
    use_proxy(
        url = "gate.smartproxy.com",
        port = 7000,
        username = usname,
        password = uspwd
        ),config(forbid_reuse = TRUE)),"text"))$ip)
}