3

I have created a lyrics bot in python. I am scrapping lyrics from genius.com using robobrowser but it's not sending the lyrics to the group.

if ( parser.getCommand() == 'GroupMESG'):
    if(parser.getPayload().lower()[:7]=='!lyrics'):
        #Send wait message in the group
        parser.sendGroupMessage(parser.getTargetID(), "/me Please wait.. I am finding your song.")
        browser = RoboBrowser(history=True)
        browser.open('http://genius.com/')

        # Search for Porcupine Tree
        form = browser.get_form(action='/search')
        form                # <RoboForm q=>
        form['q'].value = parser.getPayload().lower()[8:]
        browser.submit_form(form)

        # Look up the first song
        songs = browser.select('.song_link')
        browser.follow_link(songs[0])
        lyrics = browser.select('.lyrics')
        lyrics[0].text

        print(lyrics[0].text) ## it prints the songs

        ##it is not sending lyrics in the group :(
        parser.sendGroupMessage(parser.getTargetID(), lyrics[0].text)
Bharat
  • 163
  • 1
  • 2
  • 9

3 Answers3

2

To get rid of the warning follow the advice from the issue discussion on github.

Pass the parser to the RoboBrowser constructor:

browser = RoboBrowser(parser='html.parser')
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • 1
    Thanks man.. It solved warning but still my program can't send the lyrics to the instant messenger server. It can print the lyrics on the command line then why it can't send it on the server? – Bharat Nov 15 '16 at 08:48
  • 2
    If your main problem is sending text to a group, forget about the screen scraping and just focus on the instant messenger. Reduce your example to as small as possible. See how to create a [mcve]. – Peter Wood Nov 15 '16 at 08:50
0

my personal solution (although prone to error (if you incorrectly type the artist name, or song, you will not get a result))

import bs4, requests

song = input('Input artist then song name\n')
website = requests.get(f'https://genius.com/{song.replace(" ", "-")}-lyrics')
websiteParser = bs4.BeautifulSoup(website.text, 'html.parser')
lyricSelect = websiteParser.select('.lyrics')
lyrics = lyricSelect[0].text.strip()
print(lyrics)

This uses the way genius links are formatted to its advantage: artist name, then the song name, all connected with -. The input asks for all this information divided by spaces which are then .replaced() with - and inserted into the link which is then parsed for the lyrics which are always under the css selector '.lyrics', then it strips the html text of tags and just prints lyrics as text.

NullableMoh
  • 280
  • 3
  • 4
-1

Is that a typo in the second line? This probably gives you an error in the python code.

form = browser.get_form(action='/search')
form                # <RoboForm q=>
form['q'].value = parser.getPayload().lower()[8:]
KlemensE
  • 130
  • 2
  • 6
  • [browser.get_form()] returns a [Tag](https://www.crummy.com/software/BeautifulSoup/bs4/doc/#tag) from an HTML [tag](https://www.crummy.com/software/BeautifulSoup/bs4/doc/#calling-a-tag-is-like-calling-find-all). The second line seems useless, but correct. – jalanb May 05 '18 at 22:15