R - 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.