guoguo12 / billboard-charts

Python API for downloading Billboard charts.
https://pypi.python.org/pypi/billboard.py
MIT License
387 stars 116 forks source link

Lyrics in search results? #68

Closed redapemusic35 closed 4 years ago

redapemusic35 commented 4 years ago

Is there a way to include lyrics in the search results? <a class="chart-element__information__lyrics show-more__hidden-el" title="Read Lyrics of Blinding Lights by The Weeknd" href="https://www.billboard.com/articles/news/lyrics/8545919/the-weeknd-blinding-lights-lyrics" target="_blank">Song Lyrics</a> I guess that the following is the html element containing the address for the lyrics. Would I add "chart-element_information_lyrics show-more_hidden-el" to the css selector?

guoguo12 commented 4 years ago

Did you figure this out? If not, can you clarify what you're trying to do?

redapemusic35 commented 4 years ago

You can return various lists, titles, artists, time song spent in top 10, 100 etc. I was just wondering if it is possible to include the ability to return song lyrics in addition to the other variables. Something like:

>>> song = chart[0]  # Get no. 1 song on chart
>>> song.title
'Nice For What'
>>> song.artist
'Drake'
>>> song.weeks  # Number of weeks on chart
2
>>> song.lyric
'blah blah blah'
guoguo12 commented 4 years ago

Oh, gotcha. You need to write a selector for the a element, grab the URL, download that page, and parse it.

Shinyhero36 commented 4 years ago

You can also use the Musixmath API.

redapemusic35 commented 4 years ago

I am really a novice when it comes to coding generally, what is a Musixmath API?

Ok I see. Though there is a free version, it appears as though it only gives access to 30% of the lyrics, and I do not have the funding for the paid version.

Shinyhero36 commented 4 years ago

I am really a novice when it comes to coding generally, what is a Musixmath API?

What you can do is somthing like this:

import requests
from bs4 import BeautifulSoup as Parse

def make_soup(url):
    """
    Parse a web page info html
     """
    user_agent = {
        'User-Agent': "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
    }
    r = requests.get(url, headers=user_agent)
    html = Parse(r.content, "html.parser")
    return html

def format_url(string):
    """
    Replace les spaces with '%20'
    """
    return string.replace(" ", "%20")

def get_song_url(html):
    song_url = html.find("a", {"class": "title"})["href"]
    return song_url

def find_Lyrics(titre, artiste):
        url = f"https://www.musixmatch.com/fr/search/{artiste}%20{titre}/tracks"

        url = format_url(url)                
        pageweb = make_soup(url)    

        # Recupere le lien de la chanson
        song_url = pageweb.find("a", {"class": "title"})["href"]
        song_url = "https://www.musixmatch.com" + song_url

        # Recupere les paroles
        pageweb = make_soup(song_url)
        paroles = list()
        for span in pageweb.find_all("span", {"class" : "lyrics__content__ok"}):
            print(span.text)

find_Lyrics("title","artist")

@guoguo12 @redapemusic35

This is an old code of mine. Didn't test it but this shoud do what you requested

redapemusic35 commented 4 years ago

Okay thank you so much for this is great!