Now that you've read and seen some docmentation regarding the use of Beautiful Soup, its time to practice and put that to work! In this lab you'll formalize some of our example code into functions and scrape the lyrics from an artist of your choice.
You will be able to:
- Scrape Static webpages
- Select specific elements from the DOM
Write a function to collect the links to each of the song pages from a given artist page.
#Starter Code
from bs4 import BeautifulSoup
import requests
url = '' #Put the URL of your AZLyrics Artist Page here!
html_page = requests.get(url) #Make a get request to retrieve the page
soup = BeautifulSoup(html_page.content, 'html.parser') #Pass the page contents to beautiful soup for parsing
#The example from our lecture/reading
data = [] #Create a storage container
for album_n in range(len(albums)):
#On the last album, we won't be able to look forward
if album_n == len(albums)-1:
cur_album = albums[album_n]
album_songs = cur_album.findNextSiblings('a')
for song in album_songs:
page = song.get('href')
title = song.text
album = cur_album.text
data.append((title, page, album))
else:
cur_album = albums[album_n]
next_album = albums[album_n+1]
saca = cur_album.findNextSiblings('a') #songs after current album
sbna = next_album.findPreviousSiblings('a') #songs before next album
album_songs = [song for song in saca if song in sbna] #album songs are those listed after the current album but before the next one!
for song in album_songs:
page = song.get('href')
title = song.text
album = cur_album.text
data.append((title, page, album))
data[:2]
Write a secondary function that scrapes the lyrics for each song page.
#Remember to open up the webpage in a browser and control-click/right-click and go to inspect!
from bs4 import BeautifulSoup
import requests
#Example page
url = 'https://www.azlyrics.com/lyrics/lilyallen/sheezus.html'
html_page = requests.get(url)
soup = BeautifulSoup(html_page.content, 'html.parser')
soup.prettify()[:1000]
Create a script using your two functions above to scrape all of the song lyrics for a given artist.
#Use this block for your code!
Generate two bar graphs to compare lyrical changes for the artist of your chose. For example, the two bar charts could compare the lyrics for two different songs or two different albums.
#Use this block for your code!
Think about how you structured the data from your web scraper. Did you scrape the entire song lyrics verbatim? Did you simply store the words and their frequency counts, or did you do something else entirely? List out a few different options for how you could have stored this data. What are advantages and disadvantages of each? Be specific and think about what sort of analyses each representation would lend itself to.
#Use this block for your code!
Congratulations! You've now practiced your Beautiful Soup knowledge!