Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use regular expression for new_div's class name #154

Merged
merged 3 commits into from
Sep 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions lyricsgenius/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,25 +385,17 @@ def _scrape_song_lyrics_from_url(self, url):

# Determine the class of the div
old_div = html.find("div", class_="lyrics")

if old_div:
lyrics = old_div.get_text()
else:
lyrics = ''
for tag in html.find_all('div'):
for attribute, value in list(tag.attrs.items()):
if attribute == 'class' and 'Lyrics__Root' in str(value):
lyrics = tag
break
if lyrics:
break
new_div = html.find("div", class_=re.compile("Lyrics__Root"))
if new_div:
lyrics = new_div.get_text('\n').replace('\n[', '\n\n[')
else:
if self.verbose:
print("Couldn't find the lyrics section.")
return None

lyrics = lyrics.get_text('\n').replace('\n[', '\n\n[')

if self.remove_section_headers: # Remove [Verse], [Bridge], etc.
lyrics = re.sub(r'(\[.*?\])*', '', lyrics)
lyrics = re.sub('\n{2}', '\n', lyrics) # Gaps between verses
Expand Down