How to Download Instagram Posts Using Python

 

How This Python Code Downloads Instagram Posts

If you want to download an Instagram post using Python, this code makes it super simple with the help of a package called instaloader. Let’s understand how it works step by step.


✅ First, Install the Required Package

To make this code work, you only need one package:

pip install instaloader

This command installs the Instaloader library, which allows Python to download Instagram content like posts, reels, profile pictures, etc.


๐Ÿงพ Full Code Provided:

import instaloader
def download_instagram_post(post_url):
loader = instaloader.Instaloader()
try:
parts = post_url.rstrip('/').split('/')
shortcode = parts[-2]
except Exception as e:
print(f"Error extracting shortcode: {e}")
return
try:
post = instaloader.Post.from_shortcode(loader.context, shortcode)
loader.download_post(post, target="downloads")
print(f"✅ Successfully downloaded post: {shortcode}")
except instaloader.exceptions.InstaloaderException as e:
print(f"❌ Failed to download post: {e}")
if __name__ == "__main__":
url = input("Enter Instagram post URL: ").strip()
download_instagram_post(url)

๐Ÿช› How This Code Works — Step-by-Step

๐Ÿ”น Step 1: Import the instaloader Library

import instaloader
  • This line loads the Instaloader module into your code.
  • Without this, you cannot access Instagram content via Python.

๐Ÿ”น Step 2: Define a Function download_instagram_post

def download_instagram_post(post_url):
  • This function is the main logic block that takes an Instagram post URL as input.

๐Ÿ”น Step 3: Create an Instaloader Object

loader = instaloader.Instaloader()
  • This line creates an instance of Instaloader that handles downloading.
  • Think of it like a smart Instagram downloader tool.

๐Ÿ”น Step 4: Extract the Post Shortcode from the URL

parts = post_url.rstrip('/').split('/')
shortcode = parts[-2]
  • Instagram URLs look like this
  • ruby
  • CopyEdit
  • https://www.instagram.com/p/Cl7E9d1P3Xp/
  • The shortcode is the unique ID (Cl7E9d1P3Xp) of the post.
  • This code splits the URL and extracts that shortcode for further use.

๐Ÿ”น Step 5: Load the Post Using the Shortcode

post = instaloader.Post.from_shortcode(loader.context, shortcode)
  • This tells Instaloader: “I want to download the post with this shortcode.”
  • It uses Instagram’s public API to fetch the post’s data.

๐Ÿ”น Step 6: Download the Post

loader.download_post(post, target="downloads")
  • This downloads the Instagram post and saves it into a folder named downloads.
  • If the folder doesn’t exist, it will be created automatically.

๐Ÿ”น Step 7: Get the Post URL from the User

if __name__ == "__main__":
url = input("Enter Instagram post URL: ").strip()
download_instagram_post(url)
  • This part runs when you execute the script.
  • It asks the user to paste the Instagram URL, and then calls the function to download the post.

๐Ÿ’ป How to Run This Code in VS Code

  1. Open VS Code
  2. Create a new Python file, e.g., instagram_downloader.py
  3. Paste the code inside that file
  4. Open a terminal inside VS Code (press Ctrl + ~)
  5. Run this command to install the package:
  • bash
  • CopyEdit
  • pip install instaloader
  1. Now run your script:
  • bash
  • CopyEdit
  • python instagram_downloader.py


  1. It will ask:
  • mathematica
  • CopyEdit
  • Enter Instagram post URL:
  1. Paste any public Instagram post link, like:
  1. Press Enter, and the post will be downloaded into a folder named downloads.

๐Ÿ“ What Gets Downloaded?


  • The image or video of the post
  • .txt file with the caption
  • Metadata like upload date and hashtags

Comments