Instaloader

4.15.1 · active · verified Thu Apr 16

Instaloader is an open-source Python tool designed to download pictures and videos, along with their captions and other metadata, from Instagram. It's a popular choice for data hoarders, researchers, and backup enthusiasts for automated archiving of public or private Instagram accounts and hashtags. The library is actively maintained, with its current version being 4.15.1, and releases occurring periodically to adapt to Instagram's changes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate Instaloader, optionally log in using environment variables (recommended for security and to handle private profiles/rate limits), and then download posts from a specified public profile. The login attempts to reuse a saved session file, falling back to a fresh login if no file is found.

import instaloader
import os

# Instantiate Instaloader
L = instaloader.Instaloader()

# Get username and password from environment variables for security
USERNAME = os.environ.get('INSTA_USERNAME', 'your_instagram_username')
PASSWORD = os.environ.get('INSTA_PASSWORD', 'your_instagram_password')

# Check if login credentials are provided and attempt login
if USERNAME and PASSWORD and USERNAME != 'your_instagram_username':
    try:
        L.load_session_from_file(USERNAME) # Try to load existing session
    except FileNotFoundError: # If no session file, log in
        try:
            L.login(USERNAME, PASSWORD)
            L.save_session_to_file() # Save session for future use
        except Exception as e:
            print(f"Login failed: {e}. You might need to log in via browser and complete a checkpoint, or provide correct credentials.")
            exit()
else:
    print("Warning: INSTA_USERNAME and INSTA_PASSWORD environment variables not set. Cannot download private profiles or bypass aggressive rate limits.")

# Example: Download a public profile (replace 'instagram' with target username)
target_profile_name = 'instagram'
print(f"Downloading posts for profile: {target_profile_name}")
try:
    profile = instaloader.Profile.from_username(L.context, target_profile_name)
    for post in profile.get_posts():
        L.download_post(post, "#" + target_profile_name)
    print(f"Successfully downloaded posts for {target_profile_name}")
except Exception as e:
    print(f"Error downloading profile {target_profile_name}: {e}")

view raw JSON →