尝试使用rvest进行网络抓取时,如何修复'UseMethod(“ xml_find_all”)中的错误

我是R的初学者,我正在尝试编写一个函数来从网站上某个歌手中刮取所有歌曲歌词,并返回带有歌词和歌曲名称的标题。我已经设法获得了所有歌曲的链接,但是我一直试图编写一个函数来实际获取歌词。

有问题的网站是:https : //www.letras.mus.br/belchior/44457/

歌曲标题的选择器: #js-lyric-cnt > article > div.cnt-head.cnt-head--l > div.cnt-head_title > h1

歌曲歌词的选择器: #js-lyric-cnt > article > div.cnt-letra-trad.g-pr.g-sp > div.cnt-letra.p402_premium

我写了这个函数:

get_lyrics <- function(url){
  url %>% read_html() %>% 
    um <- html_nodes('#js-lyric-cnt > article > div.cnt-letra-trad.g-pr.g-sp > div.cnt-letra.p402_premium')  
    um %>% 
    lyrics <- html_text()
  url %>% read_html() %>%
    dois <- html_nodes('#js-lyric-cnt > article > div.cnt-head.cnt-head--l > div.cnt-head_title > h1') 
    dois %>% 
    title <- html_text()
  data_frame(title, lyrics)
}

但是当我尝试运行时,我得到:

 get_lyrics('https://www.letras.mus.br/belchior/1391391/')
 Error in UseMethod("xml_find_all") : 
  no applicable method for 'xml_find_all' applied to an object of class "character" 

我不确定该如何解决,因此感谢您的帮助。

QHarr

您可以缩短选择器(通常更快,更稳定)。read_html然后只处理一次。我假设(周)-您想要一个数据帧,标题有1个条目,而歌词有1个相应条目。歌词在pclass的父元素的标签内cnt-letra; 此外,各个歌词行是br标签分隔的。为了在解析为单个字符串时保留原始歌词行间距的感觉,我添加了'\ n'来考虑这些中断。

我得到了必要的功能,以解决缺乏br在处理rvest从@rentrop这里-但正如这个问题是很老,也许我已经错过了加入这个功能呢?

链接方法以确保流程符合预期时,请谨慎使用顺序。

library(rvest)
library(magrittr)

html_text_collapse <- function(x, trim = FALSE, collapse = "\n"){
  UseMethod("html_text_collapse")
}

html_text_collapse.xml_nodeset <- function(x, trim = FALSE, collapse = "\n"){
  vapply(x, html_text_collapse.xml_node, character(1), trim = trim, collapse = collapse)
}

html_text_collapse.xml_node <- function(x, trim = FALSE, collapse = "\n"){
  paste(xml2::xml_find_all(x, ".//text()"), collapse = collapse)
}


get_lyrics <- function(url){
    page <- read_html(url)
    lyrics <- toString(page %>% html_nodes('.cnt-letra p') %>% html_text_collapse) 
    title <- page %>% html_node('.cnt-head_title') %>% html_text()
    return(data.frame(title, lyrics))
}

get_lyrics('https://www.letras.mus.br/belchior/44457/')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

读取 read_html 中的列表时,在 UseMethod("xml_find_all") 中出现 rvest 错误

方法分派时UseMethod中的错误

使用Rvest进行Web爬网,UseMethod(“ xml_find_first”)中的错误:'xml_find_first'的任何方法均不适用于“字符”类的对象

UseMethod 中的 SparkR 错误(“预测”)

如何结合tryCatch和UseMethod?

Roadoi中UseMethod(“ http_error”)中的错误

4chan:找不到带有 xml_find_all 和 rvest 的节点

UseMethod()vs inherits()确定R中对象的类

如何合并数据框?UseMethod(“ tbl_vars”)中的错误:没有适用于“ tbl_vars”的适用方法应用于“列表”类的对象

使用Python进行网络抓取时如何删除熊猫数据框中的字符?

如何在使用python进行网络抓取时访问类HTML中的特定对象

R:UseMethod(“ group_by_”)中的错误:应用于类的对象

访问Rstudio中具有“ useMethod(“ packagefunction”)的函数的完整源代码?

pmap 中的错误 UseMethod("filter_") 中的错误:“filter_”没有适用的方法应用于“character”类的对象

UseMethod调用的方法的范围

使用httr进行网络抓取时出现xml_nodeset错误

UseMethod(“ compute”)中的错误:没有适用于适用于“ nn”类对象的“ compute”适用方法

UseMethod(“ escape”)中的错误:没有适用于'escape'的适用方法应用于类的对象

UseMethod(“ group_by_”)中的错误:没有适用于'group_by_'的适用方法应用于类“ list”的对象

UseMethod("predict") 中的错误:没有适用于应用于类 "c('double', 'numeric') 的对象的 'predict' 的方法

UseMethod("filter_") 中的错误:没有适用于“filter_”的方法应用于类“function”的对象

UseMethod("SK") 中的错误:没有适用于“SK”的方法应用于“字符”类的对象

UseMethod("html_table") 中的错误:没有适用于“html_table”的方法应用于“xml_missing”类的对象

使用python进行网络抓取中的“AttributeError”

使用RVest进行网络抓取时出现R内存问题

UseMethod("select") 中的錯誤:沒有適用於 'select' 的方法應用於類 "c('integer', 'numeric')"

定义运算符不再起作用(UseMethod(“%op%”)中的错误:'%op%'的适用方法不适用于类“ character”的对象)

ggsave():UseMethod(“ grid.draw”)中的错误:没有适用于“ grid.draw”的适用于“字符”类对象的方法

UseMethod("required_pkgs") 中的错误:没有适用于“required_pkgs”的方法应用于“workflow”类的对象