【问题标题】:Using R to perform a web query and return URL's使用 R 执行 Web 查询并返回 URL
【发布时间】:2020-05-21 21:50:37
【问题描述】:

我对 R 比较陌生,之前从未使用它来编写 Web 查询。我想知道是否有任何预先存在的软件包可以满足我的需求。我正在尝试搜索一家公司并返回其网站的 URL。我有公司名称、地址和电话号码。有没有办法运行一个程序,根据我必须确认它是正确网站的信息来检查网站?

【问题讨论】:

  • 看看RCurl,这对你来说可能是个不错的选择。
  • 还有httrgithub.com/hadley/httr。它在 RCurl 之上提供了一个更加用户友好的包装器。

标签: r


【解决方案1】:

不能保证每次都能正常工作,但一定要使用 RCurl

library(RCurl)
geturlname <- function(name){
    h = getCurlHandle()
    z <- getURL(paste0("http://google.com/search?btnI=1&q=",name), # google i'm feeling lucky
        followlocation=TRUE, curl=h)
    getCurlInfo(h)$effective.url # catch the url redirect
}
geturlname("Apple")
geturlname("Google")
geturlname("Blockbuster")

【讨论】:

    【解决方案2】:

    Thomas 的函数使用 httr 更容易编写,因为它:

    • 自动为您管理句柄

    • 自动跟随重定向

    • 返回一个表示请求结果的对象

    函数如下:

    library(httr)
    geturlname <- function(name){
      url <- paste0("http://google.com/search?btnI=1&q=", name)
      GET(url)$url
    }
    geturlname("Apple")
    geturlname("Google")
    geturlname("Blockbuster")
    

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 2017-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 2018-03-10
      相关资源
      最近更新 更多