【问题标题】:R dplyr mutate with HTML (or XML) documents, nodes, or node setsR dplyr 与 HTML(或 XML)文档、节点或节点集发生变异
【发布时间】:2016-05-08 10:48:13
【问题描述】:

我有一个包含多个 HTML 链接的文件,现在想使用 dplyr 和 rvest 来获取每行每个链接的图像链接。

当我手动执行它时,它可以正常工作并返回该行,但是当在函数中调用相同的代码时,它会失败并出现以下错误:

错误:没有适用于 'xml_find_all' 的方法应用于 类“因素”

我不知道我做错了什么。任何帮助表示赞赏。为了让我的问题更清楚,我添加了(在 cmets 中)一些示例行,并展示了手动方法。

library(rvest)
library(dplyr)
library(httr) # contains function stop_for_status()

#get html links from file
#EXAMPLE

# "_id",url

# 560fc55c65818bee0b77ec33,http://www.seriouseats.com/recipes/2011/01/sriracha-ceviche-recipe.html
# 560fc57e65818bee0b78d8b7,http://www.seriouseats.com/recipes/2008/07/pasta-arugula-tomatoes-recipe.html
# 560fc57e65818bee0b78dcde,http://www.seriouseats.com/recipes/2007/08/cook-the-book-minty-boozy-chic.html
# 560fc57e65818bee0b78de93,http://www.seriouseats.com/recipes/2010/02/chipped-beef-gravy-on-toast-stew-on-a-shingle-recipe.html
# 560fc57e65818bee0b78dfe6,http://www.seriouseats.com/recipes/2011/05/dinner-tonight-quinoa-salad-with-lemon-cream.html
# 560fc58165818bee0b78e65e,http://www.seriouseats.com/recipes/2010/10/dinner-tonight-spicy-quinoa-salad-recipe.html

#
#load into SE
#
SE <- read.csv("~/Desktop/SeriousEats.csv")

#
#function to retrieve imgPath per URL
#using rvest
#      
getImgPath <- function(x) {

  imgPath <- x %>% html_nodes(".photo") %>% html_attr("src")
  stop_for_status(res)
  return(imgPath)
}

#This works fine
#UrlPage <- read_html ("http://www.seriouseats.com/recipes/2011/01/sriracha-ceviche-recipe.html")
#imgPath <- UrlPage %>% html_nodes(".photo") %>% html_attr("src")

#
#This throws an error msg
#
S <- mutate(SE, imgPath = getImgPath(SE$url))

【问题讨论】:

  • 试试mutate(SE, imgPath = getImgPath(url))。我认为通过使用$,当mutate 期望逐行执行时,您正在引用整列
  • 另外,请dput 你的SE 对象(或至少一部分)。
  • 网址是否被视为因素?在read.csv 命令中尝试stringsAsFactors=F
  • 不。错误:没有适用于 'xml_find_all' 的方法应用于“字符”类的对象
  • 你的getImgPath() 函数中的res 是什么?我没有看到它在您的代码中的任何位置分配

标签: r dplyr rvest


【解决方案1】:

这行得通:

library(rvest)
library(dplyr)

# SE <- data_frame(url = c(
#    "http://www.seriouseats.com/recipes/2011/01/sriracha-ceviche-recipe.html",
#    "http://www.seriouseats.com/recipes/2008/07/pasta-arugula-tomatoes-recipe.html"
# ))

SE <- read.csv('/path/to/SeriousEats.csv', stringsAsFactors = FALSE)

getImgPath <- function(x) {
    # x must be "a document, a node set or a single node" per rvest documentation; cannot be a factor or character
    imgPath <- read_html(x) %>% html_nodes(".photo") %>% html_attr("src")
    # httr::stop_for_status(res) OP said this is not necessary, so I removed
    return(imgPath)
}

S <- SE %>% 
    rowwise() %>%
    mutate(imgPath = getImgPath(url))

【讨论】:

  • 感谢 :) 捕捉到这一点。
  • @DirkLX 我更新了rowwise() 以使其在文件的每一行都工作
  • @Jubbles 抱歉,但我仍然很困惑。你的第一行对我来说毫无意义,因为我需要从我的文件中获取数据。我已将其更改为此 SE
【解决方案2】:

感谢您的帮助和耐心以及@Jubbles。为了他人的利益,这里是完整的答案。

library(rvest)
library(dplyr)

SE <- read.csv("~/Desktop/FILE.txt", stringsAsFactors = FALSE)

getImgPath <- function(x) {

  if (try(url.exists(x))) {
  imgPath <- html(x) %>% 
            html_nodes(".photo") %>% 
              html_attr("src")
} 
else {
imgPath = "NA"
}
 #imgPath
 return(imgPath)
}

SE1 <- SE %>% 
  rowwise() %>%
  mutate(imgPath = getImgPath(url))

【讨论】:

    猜你喜欢
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 2011-03-10
    • 1970-01-01
    相关资源
    最近更新 更多