import glob import eyed3 import urllib2 from bs4 import BeautifulSoup song_name= glob.glob("*.mp3") for name in song_name: try: print ((name)) str = name str = unicode(str, errors='replace') name = str.encode('utf8') audiofile = eyed3.load((name)) print('Test') name= name.replace('.mp3','') print('Applying to '+name) name = name +' lyrics' name = name.replace(' ','+') url = 'http://www.google.com/search?q='+name req = urllib2.Request(url, headers={'User-Agent' : "foobar"}) response = urllib2.urlopen(req) str = response.read() str = unicode(str, errors='replace') #print(str.encode('utf8')) result = str.encode('utf8') link_start=result.find('http://www.metrolyrics.com') link_end=result.find('html',link_start+1) #print(result[link_start:link_start+57]) link = result[link_start:link_end+4] lyrics_html = urllib2.urlopen(link).read() soup = BeautifulSoup(lyrics_html), "html.parser" raw_lyrics= (soup.findAll('p', attrs={'class' : 'verse'})) paras=[] test1=unicode.join(u'\n',map(unicode,raw_lyrics)) test1= (test1.replace('<p class="verse">','\n')) test1= (test1.replace('<br/>',' ')) test1 = test1.replace('</p>',' ') print (test1) audiofile.tag.lyrics.set(u''+test1 ) audiofile.tag.save() print('lyrics Added! ') except : print ('An error occured for '+name)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question