简体   繁体   English

使用 Geniusr 从歌曲中获取歌词

[英]Get lyrics from song with Geniusr

I have the package geniusr which has different functions to retrieve lyrics from songs.我有 package 天才,它具有从歌曲中检索歌词的不同功能。 If I type following code:如果我输入以下代码:

get_lyrics_search("Bloc Party", "Signs")

which is song title and artist.这是歌名和艺术家。 But I only get "A tibble:0 x 5" as an output.但我只得到“A tibble:0 x 5”作为 output。 Does anyone know what could possibly be wrong?有谁知道可能出了什么问题?

There is already an issue raised in github . github 中已经出现了一个问题 If we do the fix specified如果我们进行指定的修复

get_lyrics <- function (session) {
  lyrics <-  session %>% html_nodes(xpath = '//div[contains(@class, "Lyrics__Container")]')
  song <-  session %>% html_nodes(xpath = '//span[contains(@class, "SongHeaderVariantdesktop__")]') %>% html_text(trim = TRUE)
  artist <-  session %>% html_nodes(xpath = '//a[contains(@class, "SongHeaderVariantdesktop__Artist")]') %>% html_text(trim = TRUE)
  xml_find_all(lyrics, ".//br") %>% xml_add_sibling("p", "\n")
  xml_find_all(lyrics, ".//br") %>% xml_remove()
  lyrics <- html_text(lyrics, trim = TRUE)
  lyrics <- unlist(strsplit(lyrics, split = "\n"))
  lyrics <- grep(pattern = "[[:alnum:]]", lyrics, value = TRUE)
  if (is_empty(lyrics)) {
    return(tibble(line = NA, section_name = NA, section_artist = NA, 
                  song_name = song, artist_name = artist))
  }
  section_tags <- nchar(gsub(pattern = "\\[.*\\]", "", lyrics)) == 0
  sections <- geniusr:::repeat_before(lyrics, section_tags)
  sections <- gsub("\\[|\\]", "", sections)
  sections <- strsplit(sections, split = ": ", fixed = TRUE)
  section_name <- sapply(sections, "[", 1)
  section_artist <- sapply(sections, "[", 2)
  section_artist[is.na(section_artist)] <- artist
  tibble(line = lyrics[!section_tags], section_name = section_name[!section_tags], 
         section_artist = section_artist[!section_tags], song_name = song, 
         artist_name = artist)
}
assignInNamespace("get_lyrics", get_lyrics, "geniusr")

and now run the code现在运行代码

> get_lyrics_search("Bloc Party", "Signs")
# A tibble: 25 × 5
   line                                   section_name section_artist song_name artist_name
   <chr>                                  <chr>        <chr>          <chr>     <chr>      
 1 Two ravens in the old oak tree         Verse 1      Bloc Party     Signs     Bloc Party 
 2 And one for you and one for me         Verse 1      Bloc Party     Signs     Bloc Party 
 3 And bluebells in the late December     Verse 1      Bloc Party     Signs     Bloc Party 
 4 I see signs now all the time           Verse 1      Bloc Party     Signs     Bloc Party 
 5 The last time we slept together        Verse 1      Bloc Party     Signs     Bloc Party 
 6 There was something that was not there Verse 1      Bloc Party     Signs     Bloc Party 
 7 You never wanted to alarm me           Verse 1      Bloc Party     Signs     Bloc Party 
 8 But I'm the one that's drowning now    Verse 1      Bloc Party     Signs     Bloc Party 
 9 I can sleep forever these days         Verse 2      Bloc Party     Signs     Bloc Party 
10 Cause in my dreams I see you again     Verse 2      Bloc Party     Signs     Bloc Party 
# … with 15 more rows

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM