为什么 url.exists returns FALSE 当 URL 确实存在时使用 RCurl?

Why url.exists returns FALSE when the URL does exists using RCurl?

例如:

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

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

> 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 settings and Proxy setting for R) 回答了同一个问题。我还没有尝试过如何从这里提取信息。