johnwmillr / LyricsGenius

Download song lyrics and metadata from Genius.com 🎶🎤
http://www.johnwmillr.com/scraping-genius-lyrics/
MIT License
892 stars 158 forks source link

Search all songs containing keyword #174

Closed ManuelBanza closed 3 years ago

ManuelBanza commented 3 years ago

Hi,

Is it possible to get all songs that have a specific word in their lyrics?

Ideally getting song name and artist.

Cheers

allerter commented 3 years ago

I think genius.search_lyrics is what you're looking for. For example:

from lyricsgenius import Genius

access_token = 'MY_ACCESS_TOKEN'
genius = Genius(access_token)

search = genius.search_lyrics("test")
for hit in search['sections'][0]['hits']:
    song = hit['result']
    highlight = hit['highlights'][0]

    lyrics = highlight['value']
    artist = song['primary_artist']['name']
    title = song['title']
    song_id = song['id']
    occurrences = len(highlight['ranges'])

    print(f"Song: {artist} - {title} | Song ID: {song_id}")
    print(f"highlighted Lyrics: {lyrics}")
    print(f"Occurrence: {occurrences} times")
    print("-" * 40)
ManuelBanza commented 3 years ago

yes this is exactly what I wanted @Allerter !

Can you also suggest a way to go through all the pages so I can get all the songs that have this specific word? I see that there is a field called next_page

Thanks

allerter commented 3 years ago

The snippet to get an artist's least popular song uses a while loop to go through all the songs of an artist using the per_page parameter. It'll be the same for traversing the pages of search_lyrics as well.

allerter commented 3 years ago

@johnwmillr this issue is a good candidate if the Discussions tab were added.