0

what is the problem with this code I am using python 2.7.2 I am trying to create a python script that can automatically look up the lyric for a song it is for a project I have been working on

import codecs
import json
import sys
import urllib
import urllib2

import bs4  

def extract_lyrics(page):
    """Extract lyrics text from given lyrics.wikia.com html page."""
    soup = bs4.BeautifulSoup(page)
    result = []
    for tag in soup.find('div', 'lyricbox'):
        if isinstance(tag, bs4.NavigableString):
            if not isinstance(tag, bs4.element.Comment):
                result.append(tag)
        elif tag.name == 'br':
            result.append('\n')
    return "".join(result)

artist = raw_input("Enter artist:")
song = raw_input("Enter song:")

query = urllib.urlencode(dict(artist=artist, song=song, fmt="realjson"))
response = urllib2.urlopen("http://lyrics.wikia.com/api.php?" + query)
data = json.load(response)

if data['lyrics'] != 'Not found':

    print(data['lyrics'])

    lyrics = extract_lyrics(urllib2.urlopen(data['url']))
    filename = "[%s] [%s] lyrics.txt" % (data['artist'], data['song'])
    with codecs.open(filename, 'w', encoding='utf-8') as output_file:
        output_file.write(lyrics)
    print("written '%s'" % filename)
else:
    sys.exit('not found')

it gives me this error

    Traceback (most recent call last):
  File "C:\Users\elaya\Desktop\Song.py", line 26, in <module>
    data = json.load(response)
  File "C:\Python27\lib\json\__init__.py", line 291, in load
    **kw)
  File "C:\Python27\lib\json\__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

please help me

1
  • 1
    Have you inspected the response variable to see if it actually returns json at all Commented Dec 16, 2016 at 11:53

1 Answer 1

1

You should look at what the response actually is. When I try that URL with those parameters, I get a mediawiki help page; reading through, it seems that you need to supply an action parameter with the value of lyrics.

query = urllib.urlencode(dict(action='lyrics', artist=artist, song=song, fmt="realjson"))
Sign up to request clarification or add additional context in comments.

1 Comment

thanks so much you saved my project I really don't know how to thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.