# Buggy code
def scrape_lyrics_from_html(html):
    """Scrape lyrics from a URL. If no lyrics can be found, return None
    instead.
    """
    from bs4 import SoupStrainer, BeautifulSoup

    if not html:
        return None

    def is_text_notcode(text):
        length = len(text)
        return (
            length > 20
            and text.count(" ") > length / 25
            and (text.find("{") == -1 or text.find(";") == -1)
        )

    html = _scrape_strip_cruft(html)
    html = _scrape_merge_paragraphs(html)

    # extract all long text blocks that are not code
    try:
        soup = BeautifulSoup(
            html, "html.parser", parse_only=SoupStrainer(text=is_text_notcode)
        )
    except HTMLParseError:
        return None

    soup = sorted(soup.stripped_strings, key=len)[-1]

    return soup


# Traceback (UnicodeEncodeError)
fetching Elephanz Stereo
Traceback (most recent call last):
File "/Users/flap/bin/beet", line 20, in <module>
beets.ui.main()
File "/Users/flap/Dev/beets/beets/ui/__init__.py", line 937, in main
_raw_main(args)
File "/Users/flap/Dev/beets/beets/ui/__init__.py", line 927, in _raw_main
subcommand.func(lib, suboptions, subargs)
File "/Users/flap/Dev/beets/beets/ui/commands.py", line 872, in import_func
import_files(lib, paths, query)
File "/Users/flap/Dev/beets/beets/ui/commands.py", line 844, in import_files
session.run()
File "/Users/flap/Dev/beets/beets/importer.py", line 300, in run
pl.run_parallel(QUEUE_SIZE)
File "/Users/flap/Dev/beets/beets/util/pipeline.py", line 299, in run
out = self.coro.send(msg)
File "/Users/flap/Dev/beets/beets/util/pipeline.py", line 181, in coro
func(*(args + (task,)))
File "/Users/flap/Dev/beets/beets/importer.py", line 1263, in plugin_stage
func(session, task)
File "/Users/flap/Dev/beets/beetsplug/lyrics.py", line 457, in imported
False, False)
File "/Users/flap/Dev/beets/beetsplug/lyrics.py", line 473, in fetch_item_lyrics
lyrics = [self.get_lyrics(artist, title) for title in titles]
File "/Users/flap/Dev/beets/beetsplug/lyrics.py", line 503, in get_lyrics
lyrics = backend(artist, title)
File "/Users/flap/Dev/beets/beetsplug/lyrics.py", line 405, in fetch_google
if is_lyrics(lyrics, artist):
File "/Users/flap/Dev/beets/beetsplug/lyrics.py", line 302, in is_lyrics
text.decode('utf8')))
File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 61: ordinal not in range(128)

# Fixed code
def scrape_lyrics_from_html(html):
    """Scrape lyrics from a URL. If no lyrics can be found, return None
    instead.
    """
    from bs4 import SoupStrainer, BeautifulSoup

    if not html:
        return None

    def is_text_notcode(text):
        length = len(text)
        return (
            length > 20
            and text.count(" ") > length / 25
            and (text.find("{") == -1 or text.find(";") == -1)
        )

    html = _scrape_strip_cruft(html)
    html = _scrape_merge_paragraphs(html)

    # extract all long text blocks that are not code
    try:
        soup = BeautifulSoup(
            html, "html.parser", parse_only=SoupStrainer(text=is_text_notcode)
        )
    except HTMLParseError:
        return None

    soup = sorted(soup.stripped_strings, key=len)[-1]
    if isinstance(soup, str):
        soup = soup.decode("utf8", "ignore")
    return soup