Skip to content

Instantly share code, notes, and snippets.

@pmacMaps
Last active May 9, 2022 20:17
Show Gist options
  • Save pmacMaps/ded6fb26bd86a4c28f275e8aae1cf97c to your computer and use it in GitHub Desktop.
Save pmacMaps/ded6fb26bd86a4c28f275e8aae1cf97c to your computer and use it in GitHub Desktop.
Extract mp3 file links and download songs from Earthbound video game
import sys
import requests
import os
from urllib.parse import urlparse
from bs4 import BeautifulSoup
# page containing links to mp3's of songs from Earthbound
music_page_url = 'http://starmen.net/mother2/music/'
# container for mp3 links
mp3_links = []
# local directory to save content
local_dir = r'C:\output'
try:
# request the url
page = requests.get(music_page_url)
# create beautiful soup object
soup = BeautifulSoup(page.content, "html.parser")
# find all anchor elements
results = soup.find_all('a')
# iterate over anchor elements
for item in results:
# if anchor tag contains '.mp3', add link to list
if '.mp3' in item['href']:
# need to combine base url with href to create full url path
mp3_links.append('{}{}'.format(music_page_url, item['href']))
# end for loop
# iterate over links and download to local machine
for url in mp3_links:
# get properties of url
the_link = urlparse(url)
# get file name
file_name = os.path.basename(the_link.path)
# create output file name
output_file = os.path.join(local_dir, file_name)
# request file at URL
response = requests.get(url)
try:
# write file to local machine
with open(output_file, mode='wb') as localfile:
localfile.write(response.content)
print('downloaded "{}"'.format(file_name))
except (Exception, EnvironmentError) as e:
print('Error downloading "{}"'.format(file_name))
continue
except (Exception, EnvironmentError) as e:
tbE = sys.exc_info()[2]
# print error message
print("Failed at Line {}".format(tbE.tb_lineno))
print("Error: {}".format(e))
finally:
print('completed running script to download music files for Earthbound')
print('check messages for any errors that may have occured')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment