我是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"
我不确定该如何解决,因此感谢您的帮助。
您可以缩短选择器(通常更快,更稳定)。read_html
然后只处理一次。我假设(周)-您想要一个数据帧,标题有1个条目,而歌词有1个相应条目。歌词在p
class的父元素的标签内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] 删除。
我来说两句