Language: Python
Web
Instagrapi was created to provide an easy-to-use and fast Python interface for Instagram automation. Unlike the official Instagram API, Instagrapi is lightweight, does not require app registration, and supports a wide range of Instagram features, making it popular among developers for personal projects, analytics, and social media automation.
Instagrapi is a modern Python library for Instagram automation. It allows you to programmatically interact with Instagram, including posting photos and stories, managing followers, reading feeds, and downloading media.
pip install instagrapiconda install -c conda-forge instagrapiInstagrapi provides methods for logging in, uploading media, interacting with followers, downloading stories, and analyzing Instagram data. It supports both personal and business accounts and includes features for handling reels, IGTV, and metadata.
from instagrapi import Client
client = Client()
client.login('your_username', 'your_password')Authenticates your Instagram account using username and password.
followers = client.user_followers('user_id')
for uid, user in followers.items():
print(user.username)Retrieves and prints the list of followers for a specific user ID.
client.photo_upload('image.jpg', 'Caption for the photo')Uploads a photo to your Instagram account with the specified caption.
stories = client.user_stories('user_id')
for story in stories:
client.story_download(story.pk, f'{story.pk}.jpg')Downloads all active stories for a user and saves them locally.
client.photo_story('story_image.jpg', 'Story caption')Uploads a photo as a story to your Instagram account.
medias = client.user_medias('user_id', 10)
for media in medias:
print(media.caption_text)Retrieves the latest 10 posts from a user and prints the captions.
client.user_follow('user_id')Follows the specified user by user ID.
client.user_unfollow('user_id')Unfollows the specified user by user ID.
Use environment variables to store login credentials securely.
Respect Instagram’s terms of service and avoid aggressive automation.
Use try/except to handle network errors and account restrictions.
Rate-limit requests to prevent temporary account blocks.
Keep session files to maintain login and reduce repeated authentication.