【问题标题】:R blocks an URL?R阻止了一个URL?
【发布时间】:2021-11-11 16:16:39
【问题描述】:

在我的 Linux 机器上,我一直在开发一个为 BaseX 提供 R 客户端的软件包。
在那台机器上,所有测试都通过了。 然而,在 Windows 上执行相同的代码时,一项测试失败。 url.exists("https://raw.githubusercontent.com/BaseXdb/basex/master/basex-api/src/test/resources/first.xml") 返回 FALSE。
我检查了其他一些 URL。
url.exists("https://www.cnn.com") 被接受。 url.exists("https://nos.nl") 被拒绝。

为什么所有的 URL 都被 Linux 接受?为什么 some 会被 Windows 阻止?所有机器都在同一个家庭网络中。

【问题讨论】:

  • 试试spsUtil::checkUrl("https://nos.nl")

标签: r


【解决方案1】:

这似乎与使用旧版本 curl 的 RCurl 相关,而不是 url 被 Windows 阻止(即使使用最新版本的 RCurl,我也会遇到相同的错误)。详情请见here

与此同时,还有很多其他解决方案。比如httr这个包就很有用(httr::GET(url)在这里可以用),甚至R的内置工具也应该可以和GitHub协商——试试readLines

readLines("https://raw.githubusercontent.com/BaseXdb/basex/master/basex-api/src/test/resources/first.xml")
#> [1] "<first><title>First Document</title></first>"

【讨论】:

    【解决方案2】:

    我重写了我的函数,但现在它是基于httr的:

    library("pingr")
    library("httr")
    input_to_raw <- function(input) {
      browser()
      type <- typeof(input)
      switch (type,
              "raw"       = raw_input <- input,       # Raw
              "character" = {
                if (input == "") {                    # Empty input
                  raw_input <- raw(0)
                } else if (file.exists(input)) {      # File on filesystem
                  finfo <- file.info(input)
                  toread <- file(input, "rb")
                  raw_input <- readBin(toread, what = "raw", size = 1, n = finfo$size)
                  close(toread)
                } else if (pingr::is_online() && (get_URL <- httr::GET(input))$status ==200) {
                  raw_input <- get_URL$content
                }
                else {                                # String
                  raw_input <- charToRaw(input)
                }
              },
              default = stop("Unknown input-type, please report the type of the input."
             )
      )
      return(raw_input)
    }
    

    独立它可以按预期工作。但是在我的包中使用时,我得到了这个错误:

    Error in curl::curl_fetch_memory(url, handle = handle) : 
      URL using bad/illegal format or missing URL
    

    有什么线索吗?

    【讨论】:

    • 我认为 httr::GET 足够聪明,可以检查“输入”是否是有效的 URL,但事实证明这不是真的。在搜索验证 URL 的方法时,我发现了这个链接 https://cran.r-project.org/web/packages/rex/vignettes/url_parsing.html,它描述了一种方法。将此方法合并到我的代码中后,现在一切正常。我仍然认为 R 没有提供检查 URL 的基本功能很奇怪
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多