【问题标题】:How to fix the timeout problem in curl::curl_fetch_memory when using R with a proxy?将 R 与代理一起使用时,如何解决 curl::curl_fetch_memory 中的超时问题?
【发布时间】:2021-11-25 08:29:06
【问题描述】:

我看到很多关于curl 的超时问题的问题。但是,我仍然对我的情况感到困惑。

我在使用quantmod::getSymbols 时出现问题。每次试验都以

结束
Warning message:
x = 'AAPL', get = 'stock.prices': Error in curl::curl_fetch_memory(cu, handle = h): Timeout was reached: [finance.yahoo.com] Operation timed out after 10008 milliseconds with 0 out of 0 bytes received

注意我正在使用代理。我已尝试打开和关闭代理或运行

httr::set_config(httr::use_proxy(
  "127.0.0.1:xxxxxx", port = 8080,
  username = "xxxx", password = "****"
), override = TRUE)

但是,没有任何效果。

在对quantmod的内部细节感到困惑之后,我决定在纯curl_fetch_memory上进行实验。

  1. curl_fetch_memory的文档中的示例在我的电脑上可以正常运行
curl::curl_fetch_memory("http://httpbin.org/cookies/set?foo=123&bar=ftw")
  1. curl_fetch_memory 不适用于"https://finance.yahoo.com/"(请注意,如果没有我的代理,我无法通过网络浏览器访问该网站)

  2. 以下代码可以成功从https://finance.yahoo.com/获取结果:

curl_opts <- list(
  ssl_verifypeer = 0L,
  proxyuserpwd = "xxxx:****",
  proxyauth = as.integer(15),
  proxy = "127.0.0.1:xxxxxx",
  proxyport = 8080
)
cookie_handler <- curl::new_handle()
curl::handle_setopt(handle=cookie_handler, .list=curl_opts)

curl::curl_fetch_memory("https://finance.yahoo.com/",
                        handle = cookie_handler)

quantmod 似乎无法正确使用代理设置。但是,没有选项可以在包中的函数内设置代理。我该如何解决我的问题?

系统:

R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936 
[2] LC_CTYPE=Chinese (Simplified)_China.936   
[3] LC_MONETARY=Chinese (Simplified)_China.936
[4] LC_NUMERIC=C                              
[5] LC_TIME=Chinese (Simplified)_China.936   

# ...

【问题讨论】:

    标签: r curl proxy quantmod


    【解决方案1】:

    我想我已经找到了解决方案。您只需要打开代理并运行

    Sys.setenv(ALL_PROXY = "YOUR_PROXY_HOST")
    

    在请求之前。

    但是,目前我仍然不知道curl 是如何使用ALL_PROXY 设置的。如果有人可以提供解释,我将不胜感激。

    【讨论】:

      猜你喜欢
      • 2017-01-20
      • 1970-01-01
      • 2014-12-16
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 2013-05-18
      • 2016-03-08
      相关资源
      最近更新 更多