Download music easily using a simple Python script.

Have you ever felt the need to download music? I mean of course their are many music streaming sites available but sometimes we need to keep music offline in mp3 format which can be easily shared and runs on all multimedia devices. But to download music you probably search for some pirated site, then maybe search the song, click on download link, select quality, go through a series of ads and then finally you get the music. But what if I told you could download music easily in 320kbps quality easily using a simple python program. This program basically scraps instamp3 site which offers high quality music of all times and genres. This program uses 2 important python libraries, requests and Beautifulsoup which can be installed using pip, here is a tutorial link if you don’t know how to use pip.

Note that this program is pretty raw and basic as it was made by me in less than an hour. But this program can be easily edited to download multiple songs at a time, selecting different quality, etc given you have some knowledge about python. Here is the code:

import os, requests
from bs4 import BeautifulSoup

os.chdir("C:\\Users\\DELL\\Desktop")  # folder where you want to keep downloaded songs.
r = requests.get("http://www.instamp3.com")
baseurl = str(r.url)
name = str(input("Enter the song name: "))
name = name.replace(" ", "-").lower()
searchpage = requests.get(baseurl + "/download/" + name + ".html")
clist = []
searchsoup = BeautifulSoup(searchpage.text, "html.parser")
c = searchsoup.find("div", {"class": "meta"})
if c is None:
    print("query not found!")
else:
    for i in range(5):  # print top 5 search results if available.
        print(str(i + 1) + ") " + str(c.text.strip().replace("play", "").replace("download", "")))
        clist.append(c)
        c = c.findNext("div", {"class": "meta"})
        if c is None:
            break
    n = int(input("Enter song number from the list: "))
    n = n - 1
    links = clist[n].find("div", {"class": "d-links"})
    downpagelink = links.find("a", {"class": "downnow"})["href"]
    downres = requests.get(str(downpagelink))
    downsoup = BeautifulSoup(downres.text, "html.parser")
    c2 = downsoup.find("div", {"class": "col-sm-9 col-md-7 col-xs-12"})
    downlink = c2.find("a")["href"]
    songname = downsoup.find("h1", {"class": "title"}).text.replace("Download", "").replace("Mp3", "").strip()
    response = requests.get(str(downlink))
    print("Downloading song...please wait for some time (depending on your internet speed)")
    try:
        with open(songname + ".mp3", 'wb') as f:
            f.write(response.content)
            print("Done downloading!")
    except:
        print("ERROR! maybe there is a problem with your internet connection or the site is down.")

That’s it! When you run this program you will be prompted to input a song query to search. Then top 5 results will be displayed for that query. Enter the result number to download that song in the folder stated in line 4 of the code.
Also, I have made a video tutorial which provides explanation for this code. You can watch it at the link below.

Youtube.

You can make similar programs involving web scraping which can download videos, Ebooks, images, subtitles etc.

Leave a Reply

Your email address will not be published. Required fields are marked *