【问题标题】:Why url.exists returns FALSE when the URL does exists using RCurl?当使用 RCurl 存在 URL 时,为什么 url.exists 返回 FALSE?
【发布时间】:2016-11-02 22:44:15
【问题描述】:

例如:

if(url.exists("http://www.google.com")) {
    # Two ways to submit a query to google. Searching for RCurl
    getURL("http://www.google.com/search?hl=en&lr=&ie=ISO-8859-1&q=RCurl&btnG=Search")
    # Here we let getForm do the hard work of combining the names and values.
    getForm("http://www.google.com/search", hl="en", lr="",ie="ISO-8859-1", q="RCurl", btnG="Search")
    # And here if we already have the parameters as a list/vector.
    getForm("http://www.google.com/search", .params = c(hl="en", lr="", ie="ISO-8859-1", q="RCurl", btnG="Search"))
}

这是来自 RCurl 包手册的示例。但是,它不起作用:

> url.exists("http://www.google.com")
[1] FALSE

我发现这里有一个答案Rcurl: url.exists returns false when url does exists。它说这是因为默认的用户代理没有用。但我不明白什么是用户代理以及如何使用它。

另外,这个错误发生在我在公司工作时。我在家里尝试了相同的代码,它找到了。所以我猜这是因为代理。或者还有其他一些我没有意识到的原因。

我需要使用 RCurl 从 Google 搜索我的查询,然后从网站中提取标题和描述等信息。在这种情况下,如何使用用户代理?或者,包 httr 可以做到这一点吗?

【问题讨论】:

  • 我无法重现此问题。 url.exists("http://www.google.com") [1] TRUE您的问题与用户代理无关。该问题特定于该问题中的服务器,当然不适用于 Google.com。当您在无法通过 R 访问 Google 的计算机上时,您是在公司网络上吗?您可能需要通过公司代理或 VPN 路由 R 的请求。
  • 是的,这就是我的猜测。我想我不能使用它,因为我使用的是公司网络。那么你能告诉我如何通过公司代理或VPN来做到这一点吗?我知道 RCurl 中有一个名为 proxy 的参数。我只是不知道如何设置。
  • 这取决于您的操作系统、代理或 VPN 以及具体配置。如果您不了解情况,最简单的方法可能是联系您公司的 IT 帮助台。您可能能够使用该选项,但它可能会更好,并且可能需要代理/隧道所有 R 或 RStudio 而不仅仅是这一功能。
  • 您是否尝试过通过指定.header = TRUE返回头文件?

标签: r url user-agent rcurl


【解决方案1】:

伙计们。非常感谢您的帮助。我想我只是想出了如何去做。重要的是代理。如果我使用:

> opts <- list(
     proxy         = "http://*******",
     proxyusername = "*****", 
     proxypassword = "*****", 
     proxyport     = 8080
)
> url.exists("http://www.google.com",.opts = opts)
[1] TRUE

然后一切都完成了!使用win 10可以在System-->proxy下找到你的代理。同时:

 > site <- getForm("http://www.google.com.au", hl="en",
                 lr="", q="r-project", btnG="Search",.opts = opts)
 > htmlTreeParse(site)
 $file
 [1] "<buffer>"
 .........

在 getForm 中,也需要放入 opts。这里有两张海报(RCurl default proxy settingsProxy setting for R)回答了同样的问题。我还没有尝试过如何从这里提取信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-22
    • 2020-10-07
    • 2016-11-14
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    相关资源
    最近更新 更多