curl::curl_fetch_memory(url, handle = handle) 中的错误:URL 使用错误/非法格式或缺少 URL

问题描述 投票:0回答:1

我有这个功能无法运行。代码内部的命令在单独应用时有效,但是当我在此 lapply 函数中运行它时,它会出错。我尝试过更新我的 Curl 库等...不知道为什么会发生。

    library(curl)
    ##dataframe looks like this as factor list: x = "http://google.com" 
    funk <- function(x) {
            read_html(x) %>% html_node("title") %>% html_text 
    }
    df$titles<-lapply(df$urls,funk)

    Error: 'Error in curl::curl_fetch_memory(url, handle = handle) : 
    URL using bad/illegal format or missing URL
    ' does not exist in current working directory ('/Users/Home/').

如有任何帮助,我们将不胜感激。

r curl rvest
1个回答
0
投票

该功能在内部工作

lapply
:

df <- data.frame(urls=c("http://google.com", "http://ytcracker.com"), stringsAsFactors = F)
funk <- function(x) {
  read_html(x) %>% html_node("title") %>% html_text 
}

lapply(df$urls, funk)
[[1]]
[1] "Google"

[[2]]
[1] "ytOS/2014"
df$titles <- lapply(df$urls, funk)
df
                  urls    titles
1    http://google.com    Google
2 http://ytcracker.com ytOS/2014

所以问题一定出在你的问题的不可重现的部分,这将是数据框架(请

dput()
将来你在R问题中的数据——查看R标签描述以获取更多信息)。

具体来说,错误消息暗示问题可能在于您如何在该数据框中指定 URL。

更具体地说,当您收到错误消息时

' does not exist in current working directory ('/Users/Home/').

这几乎总是意味着您没有使用

http://
https://
等来限定域名。

© www.soinside.com 2019 - 2024. All rights reserved.