Python Instagram Photo Downloader Project

Python project 16

Python Instagram Photo Downloader Project

Create a responsible Instagram post-media downloader with the maintained Instaloader package. The program validates a post or reel URL, extracts its shortcode, saves images to a dedicated folder, and reports private, unavailable, or rate-limited content clearly.

Learn Python with mentor-guided projectsView all project ideas

What you will build

The user supplies a complete public post or reel URL and an output folder. The app downloads image media and video thumbnails for that single post while disabling comments, geotags, captions, metadata JSON, and full video downloads.

Skills practised

  • Third-party Python packages
  • URL parsing and regular expressions
  • Paths and folders
  • Service-error handling

Requirements

  • Python 3.10 or later
  • Instaloader: pip install instaloader
  • An internet connection
  • Permission to save the selected media

Full Python code

Save this code as instagram_photo_downloader.py, then follow the run instructions below.

"""Download media from one Instagram post that you have permission to save."""

from __future__ import annotations

import re
from pathlib import Path
from urllib.parse import urlsplit

import instaloader


SHORTCODE_PATTERN = re.compile(r"^[A-Za-z0-9_-]+$")


def extract_shortcode(post_url: str) -> str:
    """Extract a post or reel shortcode from a complete instagram.com URL."""
    parsed = urlsplit(post_url.strip())
    host = (parsed.hostname or "").lower()
    if host not in {"instagram.com", "www.instagram.com"}:
        raise ValueError("Use a complete instagram.com post or reel URL.")

    parts = [part for part in parsed.path.split("/") if part]
    if len(parts) < 2 or parts[0] not in {"p", "reel", "tv"}:
        raise ValueError("The URL must point to an Instagram post or reel.")

    shortcode = parts[1]
    if SHORTCODE_PATTERN.fullmatch(shortcode) is None:
        raise ValueError("The Instagram shortcode contains unexpected characters.")
    return shortcode


def download_post(post_url: str, output_dir: Path) -> bool:
    shortcode = extract_shortcode(post_url)
    output_dir.mkdir(parents=True, exist_ok=True)

    loader = instaloader.Instaloader(
        dirname_pattern=str(output_dir / "{target}"),
        filename_pattern="{shortcode}_{date_utc:%Y-%m-%d_%H-%M-%S}",
        download_videos=False,
        download_video_thumbnails=True,
        download_comments=False,
        download_geotags=False,
        save_metadata=False,
        post_metadata_txt_pattern="",
        quiet=True,
    )
    post = instaloader.Post.from_shortcode(loader.context, shortcode)
    return loader.download_post(post, target=shortcode)


def main() -> None:
    print("Instagram Photo Downloader")
    print("Only download your own posts or media you have permission to save.\n")
    post_url = input("Public post or reel URL: ").strip()
    output_dir = Path(input("Output folder [instagram_downloads]: ").strip() or "instagram_downloads")

    try:
        changed = download_post(post_url, output_dir)
    except ValueError as error:
        print(f"Invalid URL: {error}")
    except instaloader.exceptions.InstaloaderException as error:
        print(f"Instagram could not provide that post: {error}")
        print("The post may be private, unavailable, rate-limited, or require a logged-in session.")
    except OSError as error:
        print(f"Could not save the download: {error}")
    else:
        print("Download complete." if changed else "The media was already downloaded.")


if __name__ == "__main__":
    main()

How the project works

extract_shortcode() accepts only Instagram post, reel, or legacy TV paths and rejects lookalike domains. Instaloader then creates a Post from that shortcode and handles the media request.

Instagram access rules and page availability can change. Public content may still require a logged-in session or become rate-limited. This example does not bypass privacy settings, authentication, or technical restrictions and should only be used for the learner’s own content or media they have explicit permission to download.

Run the project

  1. Install Instaloader with python -m pip install instaloader.
  2. Run python instagram_photo_downloader.py.
  3. Paste a complete public post or reel URL you are allowed to save.
  4. Enter an output folder or press Enter for instagram_downloads.

Accuracy and safety notes

  • Rights: permission and copyright rules still apply to downloaded media.
  • Privacy: the code does not bypass private accounts.
  • Rate limits: repeated requests can be restricted by Instagram.
  • Scope: videos are disabled; a reel thumbnail may be saved instead.

Ways to extend it

Add a saved Instaloader session for your own account, a clearer file inventory, or a graphical URL form. Follow the official Instaloader module guide and Instagram’s current rules.

Continue learning Python

Build the next project, return to the Softenant project library, or explore the Python programming course in Vizag for structured lessons, mentor feedback, and portfolio practice.