例如:
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搜索我的查询从谷歌,然后提取信息,如标题和描述从网站。在这种情况下,如何使用用户代理?或者,httr包能做到这一点吗?
发布于 2016-11-02 23:15:03
伙计们。非常感谢你的帮助。我想我刚想好了该怎么做。重要的是代理。如果我用:
> 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中,也需要加入选项。这里有两张海报(RCurl default proxy settings和Proxy setting for R)回答同一个问题。我还没有试过如何从这里提取信息。
https://stackoverflow.com/questions/40391047
复制相似问题