Instaloader
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
-
instaloader.exceptions.ConnectionException: Login: Checkpoint required.
cause Instagram's security systems detected suspicious login activity, requiring manual verification through a web browser.fixOpen a web browser, log in to your Instagram account, complete the required security checkpoint (e.g., entering a code, confirming identity), then close the browser and retry running Instaloader. Ensure you have a saved session file, or use `L.save_session_to_file()` after a successful interactive login. -
JSON decode fail, 403 - Forbidden.
cause Often related to Instagram API changes or outdated session cookies, preventing Instaloader from parsing responses.fixDelete your existing session file (usually `~/.config/instaloader/session-YOUR_USERNAME`) and perform a fresh login. Ensure you are using the latest version of Instaloader (`pip install --upgrade instaloader`). -
429 Too Many Requests
cause Instagram's server-side rate limits have been hit. This can happen due to too many requests in a short period, using unauthenticated access, or using IPs frequently associated with scraping (VPNs, cloud servers).fixEnsure you are logged in using `L.login()` or `--login`. If logged in, Instaloader will attempt to wait. For CLI, avoid frequent restarts and consider `--fast-update` for incremental downloads. If issues persist, wait a longer period before retrying. -
"Too many queries in the last time." (printed to console, but not an exception)
cause Instaloader's internal rate controller is detecting that Instagram's API rate limits are being approached and is proactively pausing to avoid a hard ban.fixThis is a notice, not an error, and Instaloader should resume automatically after a delay. This message is more common for unauthenticated requests or when downloading large amounts of data. Ensure you are logged in for potentially higher rate limits.
Warnings
- breaking In version 4.7, the default values for `download_geotags` and `download_comments` in the `Instaloader()` constructor changed from `True` to `False`.
- gotcha Instagram aggressively rate-limits requests, leading to '429 Too Many Requests' errors. Running multiple Instaloader instances, rapidly restarting, or using VPN/cloud IPs without login often triggers this.
- gotcha Login attempts can fail with 'Checkpoint required' errors, particularly when Instagram detects unusual activity or a new login location.
- gotcha To download content from private Instagram profiles, your account must follow the target profile, and you must log in to Instaloader with valid credentials.
Install
-
pip install instaloader
Imports
- Instaloader
import instaloader # then instaloader.Instaloader()
from instaloader import Instaloader
Quickstart
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}")