Python project – Download songs using python

Download music using Python - www.devpyjp.com

Hello and welcome to another amazing python project tutorial, In this you are going to learn how to build a python web-scraper that download .mp3 songs. This can help you a lot to build your own startup.

Are you ready guys ! lets build together.

Prerequisites to This Python project

  • Basics of python
  • Good experience with Beautifulsoup and requests libraries

well, if you don’t know what is Beautifulsoup or requests libraries or if you are a beginner then here is for you. Follow the below python tutorials which gives a clear picture of how to start web scraping using python.

web scraping with python – A simple start to scrape the web pages
How to find all links in a website using python web scraping

Ok, lets start our project which download mp3 songs using python. To start this project you need to packages, if don’t have those packages then install them by following commands.

pip install requests

pip install beautifulsoup4

These two are an amazing libraries in python. requests package is the most downloaded package by people.python requests package helps us to open urls of website and grab the content.

python beautifulsoup package helps us to grab the HTML elements and text of it. If you follow my recommended tutorials then you will get my words.

How download songs using python – Python project

First import the libraries which are necessary for this project. And choose your website where you want to download songs.

Here I am going to grab Telugu songs from telugump3audio. I choose one movie playlist here, you can choose your own and the same process for all websites, you need to change the URL.

# Importing libraries

from bs4 import BeautifulSoup
import requests


# URL of webiste to grab the songs.
url = 'https://telugump3audio.com/oh-baby-b.html'

#Get the content of the HTML page.
res = requests.get(url).content

Now we need to find the links which hold our songs or .mp3 files. I already mentioned above how to grab links of a website using python, so please check it out.

How to find all links in a website using python web scraping

soup = BeautifulSoup(res,'html.parser') #Making soup object to get HTML elements.

data = soup.find_all('a') #finding all anchor tags.

Now we are ready grab the songs. This is very important phase in this python project. Here i am giving the code of it.

for link in data:
    song_link = link['href']
    songs = link.text

    if '.mp3' in song_link:
       
        with open(songs,'wb') as f:
           
            res = requests.get(song_link)
            f.write(res.content)
            

Here i used a for loop to grab the all “href” attributes or links of website. we don’t need all of them, we need only the links which are ends with .mp3.

So i filtered them and open them as file using python files. To open those links i used again python requests package and write the content to file.

By doing above procedure we are able to download all the songs of that movie using python.

Full Python project code

from bs4 import BeautifulSoup
import requests

url = 'https://telugump3audio.com/oh-baby-b.html'


res = requests.get(url).content

soup = BeautifulSoup(res,'html.parser')

data = soup.find_all('a')


for link in data:
    song_link = link['href']
    songs = link.text

    if '.mp3' in song_link:
        print(song_link)
        with open(songs,'wb') as f:
            print('#------------------ ',songs,' downloading----------#')
            res = requests.get(song_link)
            f.write(res.content)
            
print('\n Completed.....!')

I hope it will definitely useful to you, if you like it please appreciate us through a comment below and for getting interesting python projects to subscribe to our newsletter. Thank you..!

1 thought on “Python project – Download songs using python”

Leave a Reply