Download Instagram Reels using Python

Download Instagram Reels using Python

Introduction

We will learn how to Download Instagram Reels using Python. You all are now aware that there is a number of python libraries to get creative and solve some real-life problems with code! Do you hear about libraries like Insta Bot, instaloader, and instascrape?? As their name seems, they provide Instagram API.

You can download profile pic, posts, igtv’s, highlights, and stories and keep track of comments, views, and likes not only of your favorite celebrity i.e. people with public accounts but also private accounts!
These libraries provide you with the facility to do so!

Here, let’s save reels from Instagram as they cannot be saved over the app itself, we can actually download them!
Here, we will download reels of public accounts. You can download of privately but it would require the user id and password of your Instagram account. Let’s start:

Instascrape

instascrape is a powerful and flexible Instagram scraping library for Python, providing easy-to-use and expressive tools for accessing data programmatically. It can be used for web scraping, data science, and data analysis.
In other words, you can scrape the whole profile of the user including stories, highlights, captions, pictures, comments count, view count, etc. It can also scrape JSON, and HTML embedded code and can download into your computer in mp3, mp4, jpg, or png format. It’s data analytics built using Selenium, Pandas, and other corresponding libraries. For private account users, the user must provide the account’s login id and password.

For detailed info, head to the documentation: https://instascrape.readthedocs.io/

Instaloader

Instaoader is also a free Instagram python API that is capable of fetching Instagram data for both private and public accounts. The special feature of the instaloader includes resume download of profiles if the download is interrupted. It also detects name changes in the profile. In one click it can download the whole Instagram feed of the user. For private accounts, the user must log in i.e. provide login authentication of the user’s account into code. It also has options for marking stories as seen or unseen to fetch stories.

For detailed info, head to the documentation: https://instaloader.github.io/

Now, that you get the central idea for the library, let’s head on to our program.

Steps to Download Instagram Reels using Python:

Nowadays, Reels have become a trending feature of Instagram which consists of creating, editing, and viewing short videos with sound. It cannot be downloaded. So, using the ‘instascrape’ module we will download the reels. Reels feature is the updated version of the module. For this, we will make a GUI application.

Let’s proceed:

Install instascrape module comes into the pip with command in your terminal as:

pip install instascrape

Now, import the Reel module from instascrape and download the reel by providing the download location using the code below:

from instascrape import Reel
google_reel = Reel({link})
google_reel.scrape()
google_reel.Download(fp = "{path\filename.mp4}")

This code will download the reel for sure, but not if you continue to make the request again and again, and hence, you need to pass the session id. Recently, due to the updated policies of Instagram, it is not that easy to scrape it. Hence with the above code, we need to pass the session id into the headers as follows:

SESSIONID = "{your session id}"
            headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.74 Safari/537.36 Edg/79.0.309.43",
            "cookie":f'sessionid={SESSIONID};'
            }
google_reel.scrape(headers=headers)

Note: SessionID changes every time when you log out. Make sure that you provide the id at the time when you are logged in.

Now, we have successfully completed our task of learning how to Download Instagram Reels using Python. But wait, let’s make it handier to use this application by converting our program to a GUI-based application to Download Instagram Reels in Python.

Now, let’s make a quick GUI design. Here I am using an image as a background to really make our application look attractive.

from tkinter import *
from PIL import ImageTk, Image
import tkinter.font as font
from tkinter import messagebox


root =Tk()
root.title("Instagram Reel Downloader")
root.minsize(600,500)
root.maxsize(600,500)
HEIGHT = 500
WIDTH = 600
FONT = font.Font(family ="Times New Roman", size ="18", weight ="bold")

canvas = Canvas(root,height = HEIGHT, width = WIDTH)
canvas.pack()

frame=Frame(root,bg="white")
frame.place(relwidth=1,relheight=1)

background_image = ImageTk.PhotoImage(Image.open(r"{path to the image}\imagename.jpg"))
background_label = Label(frame, image = background_image)
background_label.place(relx=-0.25,relwidth = 0.7, relheight =1)


label1 = Label(frame, text = "Download Reels in a Click!", font =FONT, bd =5, fg= "#0d1137",bg="white")
label1.place(relx = 0.48, rely = 0.1, relheight =0.1)



FONT = font.Font(family ="Times New Roman", size ="12", weight ="bold")
label2 = Label(frame, text = "Enter link address: ", font =FONT, bd =5, fg= "#e52165",bg="white")
label2.place(relx = 0.48, rely = 0.25, relheight =0.1)

entry = Entry(frame, font = FONT, fg = "#fbad50")
entry.place(relx = 0.48, rely = 0.35,relwidth=0.4, relheight = 0.05)

button1 = Button(root, text = "Download", font = FONT, bg = "pink", fg = "black", activeforeground = "pink", activebackground = "black",command=lambda:download(entry.get()))
button1.place(relx = 0.48,rely = 0.45,relwidth = 0.2, relheight = 0.06)

label2 = Label(frame, text = "Instructions: ", font =FONT, bd =5, fg= "#0d1137",bg="white")
label2.place(relx = 0.48, rely = 0.6, relheight =0.1)

FONT = font.Font(family ="Times New Roman", size ="10", weight ="bold")
TEXT="1.Only public account reels can be downloaded\n2.Enter the link address of reel from the Instagram\n3.This is not meant to be used for mischeif"
label2 = Label(frame, text = TEXT, font =FONT, bd =5, fg= "#cd486b",justify=LEFT,bg="white")
label2.place(relx = 0.48, rely = 0.7, relheight =0.1)



root.mainloop()

Now, let’s write a function to Download Instagram Reels using Python with some error handlings:

import time

def download(link):
    try:
        if (link):

            SESSIONID = "{your session id}"
            headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.74 Safari/537.36 Edg/79.0.309.43",
            "cookie":f'sessionid={SESSIONID};'
            }

            google_reel=Reel(link)
            google_reel.scrape(headers=headers)
            google_reel.download(fp=f".\\reel{int(time.time())}.mp4")
            messagebox.showinfo("Status","Reel downloaded successfully")
        else:
            messagebox.showwarning("Empty field","Please fill out the field")
    except Exception as e:
        messagebox.showerror("Error","Something went wrong. Please try again later.")
        print(e)

The session id is necessary if we try to make requests again and again. Time library is imported for the sake of filename for a reel.
NOTE: Use sessionid very carefully or it can get your Instagram account hacked. Hence, never share it! You’ll find the sessionid by login into your Instagram->right click and then click for ‘Inspect’->under ‘Applications’, look for ‘cookies’ now copy the session id. Session id will change every time you log out. So stay logged in to use the current session id or change the id into the code every time you log out and logged in.

Complete code to Download Instagram Reels using Python

from tkinter import *
from PIL import ImageTk, Image
import tkinter.font as font
from tkinter import messagebox
from instascrape import Reel
import time

root =Tk()
root.title("Instagram Reel Downloader")
root.minsize(600,500)
root.maxsize(600,500)
HEIGHT = 500
WIDTH = 600
FONT = font.Font(family ="Times New Roman", size ="18", weight ="bold")


def download(link):
    try:
        if (link):
            SESSIONID = "18614737527%3ApTLwFoXv5BZohu%3A4"
            headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.74 Safari/537.36 Edg/79.0.309.43",
            "cookie":f'sessionid={SESSIONID};'
            }

            google_reel=Reel(link)
            google_reel.scrape(headers=headers)
            google_reel.download(fp=f".\\reel{int(time.time())}.mp4")
            messagebox.showinfo("Status","Reel downloaded successfully")
        else:
            messagebox.showwarning("Empty field","Please fill out the field")
    except Exception as e:
        messagebox.showerror("Error","Something went wrong. Please try again later.")


canvas = Canvas(root,height = HEIGHT, width = WIDTH)
canvas.pack()

frame=Frame(root,bg="white")
frame.place(relwidth=1,relheight=1)

background_image = ImageTk.PhotoImage(Image.open(r"C:\Users\ASUS\OneDrive\Desktop\Prof\copyassign\instareel\insta5.jpg"))
background_label = Label(frame, image = background_image)
background_label.place(relx=-0.25,relwidth = 0.7, relheight =1)


label1 = Label(frame, text = "Download Reels in a Click!", font =FONT, bd =5, fg= "#0d1137",bg="white")
label1.place(relx = 0.48, rely = 0.1, relheight =0.1)



FONT = font.Font(family ="Times New Roman", size ="12", weight ="bold")
label2 = Label(frame, text = "Enter link address: ", font =FONT, bd =5, fg= "#e52165",bg="white")
label2.place(relx = 0.48, rely = 0.25, relheight =0.1)

entry = Entry(frame, font = FONT, fg = "#fbad50")
entry.place(relx = 0.48, rely = 0.35,relwidth=0.4, relheight = 0.05)

button1 = Button(root, text = "Download", font = FONT, bg = "pink", fg = "black", activeforeground = "pink", activebackground = "black",command=lambda:download(entry.get()))
button1.place(relx = 0.48,rely = 0.45,relwidth = 0.2, relheight = 0.06)

label2 = Label(frame, text = "Instructions: ", font =FONT, bd =5, fg= "#0d1137",bg="white")
label2.place(relx = 0.48, rely = 0.6, relheight =0.1)

FONT = font.Font(family ="Times New Roman", size ="10", weight ="bold")
TEXT="1.Only public account reels can be downloaded\n2.Enter the link address of reel from the Instagram\n3.This is not meant to be used for mischeif"
label2 = Label(frame, text = TEXT, font =FONT, bd =5, fg= "#cd486b",justify=LEFT,bg="white")
label2.place(relx = 0.48, rely = 0.7, relheight =0.1)



root.mainloop()

So, here it is!

Also, you can convert the ‘py’ file into ‘exe’ using auto-py-to-exe.

Now you can Download Instagram Reels using Python but don’t misuse them!
Also, take care of your session id, so that it cannot be shared!

Also, you can download profile photos, and feed images of your favorite celeb! Downloading profile photos will not require a session id! You can also parse whole Instagram data and convert the JSON. Try to code and practice the same.

Thank You for reading. Happy Pythoning!


Also Read:


Share:

Author: Ayush Purawr