Skip to content

Instantly share code, notes, and snippets.

@galakhov
Last active November 5, 2025 22:03
Show Gist options
  • Select an option

  • Save galakhov/44e9b9f53ac3a91344de110f917f66b0 to your computer and use it in GitHub Desktop.

Select an option

Save galakhov/44e9b9f53ac3a91344de110f917f66b0 to your computer and use it in GitHub Desktop.
This script auto follows Instagram users read from a text file with some delay. The list should be a plain text file (without any headers) with username on each line in order for the script to follow them. The script follows 10 accounts in less than a minute, then sleeps for some random time between 40 and 120 seconds (see or adjust on line 44).…
from instagrapi import Client
import time, random
USERNAME = '[email protected]'
PASSWORD = 'change_to_your_PASSWORD'
cl = Client()
try:
cl.load_settings("session.json")
cl.login(USERNAME, PASSWORD)
except Exception as ex:
print(f"Failed to login: {ex}, retrying once & saving to session.json...")
cl.login(USERNAME, PASSWORD)
cl.dump_settings("session.json")
with open("follow.txt") as my_file:
f = my_file.read().split("\n")
i = 0
for row in f:
if bool(row):
try:
row = str(row)
user_id = cl.user_id_from_username(row)
cl.user_follow(user_id)
i += 1
print(f"Followed user: {row}")
# username = cl.username_from_user_id(user_id) # commented out because of #2266, see below.
wait = random.uniform(5, 10)
print(f"Waiting for {wait:.1f} sec, total followed: {i}")
with open("success.log", "a") as log:
log.write(f"Followed user: {row}\n")
except Exception as e:
print(f"Failed to follow {row}: {e}")
with open("errors.log", "a") as log:
log.write(f"{row}: {e}\n")
continue
time.sleep(wait)
if i % 10 == 0:
pause = random.uniform(40, 120)
print(f"Sleeping for {pause:.1f} sec, total followed: {i}")
time.sleep(pause)
# If you need to collect the usernames first, use the following Chrome Extensions:
# https://chromewebstore.google.com/detail/igfollow-follower-export or
# https://phantombuster.com/automations/instagram/7175/instagram-follower-collector or alike.
# Prerequisites: https://github.com/subzeroid/instagrapi & create the follow.txt file with usernames/profiles to follow:
# username1
# username2
# username_3, etc.
#
# First install 'instagrapi' that manages the authentication process:
# pip install instagrapi
# To run the script:
# python follow_a_user.py
# knows issues: https://github.com/subzeroid/instagrapi/issues/2266
# Read instagrapi's docs for more information: https://subzeroid.github.io/instagrapi/usage-guide/user.html
#
# After some time of using the script you might get a message:
# "feedback_required: We limit how often you can do certain things on Instagram, like following people, to protect our community. Let us know if you think we made a mistake."
#
# Follow me on GitHub for more goodies: https://gist.github.com/galakhov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment