DEV Community

Wulfi
Wulfi

Posted on

Make an Instagram bot in Python

In this article we will try to make an Instagram bot using Python and Instabot. Bots are really common these days that automates the tasks such as sending messages, uploading photos, following and unfollowing users, and many more. Bots reduce our work and save our time. Today we will create an a simple instagram bot that can do:

i/ Post a photo

ii/ Follow one or more users

iii/ Unfollow one or a list of users

iv/ Unfollow everyone

v/ Count number of followers of a user

vi/ Like and comment on a post

vii/ Send messages

Instabot library:

Instabot is a Python module, which not only implements the wrapper over the Instagram API, but also various useful methods, such as login to instagram account, getting users from the data, getting media from users or from hastags, comments, likes, follows, and uploading photos.

By the way we hardly recommend you not to use your own account if you don’t need your personal data or don’t want to promote your account.

To get a quick understanding of how the bot works, lets start with trying out some of the bot functions.

Installation

$ pip  install instabot
Enter fullscreen mode Exit fullscreen mode

Login

To perform any of the method of a bot, we need to login to the instagram account. So first, import instabot library and login

# Import instabot library
from instabot import Bot

# Create a instance  of bot
bot = Bot()

# Login
bot.login(username="your_userid", password="your_password")
Enter fullscreen mode Exit fullscreen mode

1/ Post a photo:

A photo can be either in .jpg or jpeg format

from instabot import Bot

bot = Bot()

bot.login(username="your_userid", password="your_password")

img = "photo.jpg"   # give the path and name if it is in different directory
bot.upload_photo(img, caption="(...")

Enter fullscreen mode Exit fullscreen mode

2/ Follow one or more users

To follow one user, use follow()

from instabot import Bot

bot = Bot()

bot.login(username="your_userid", password="your_password")

# To follow single user.
bot.follow("username")
Enter fullscreen mode Exit fullscreen mode

To follow multiple users, use follow_users()

# To follow more user.
list_of_user = ["userId1", "userId2", "userId3", "...."]
bot.follow_users(list_of_user)

Enter fullscreen mode Exit fullscreen mode

3/ Unfollow one or a list of users

To unfollow one user we nees to use unfollow() function.

from instabot import Bot

bot = Bot()
bot.login(username="your_username", password="your_password")

# To unfollow a single user.
bot.unfollow("username")

Enter fullscreen mode Exit fullscreen mode

To unfollow multiple users, use unfollow_users()

# To unfollow more users.
unfollow_list = ["userId1", "userId2", "userId3", "..."]
bot.unfollow_users(unfollow_list)

Enter fullscreen mode Exit fullscreen mode

4/ Unfollow everyone

To unfollow every user that we follow, use the function unfollow_everyone()

from instabot import Bot

bot = Bot()
bot.login(username="your_username", password="your_password")

# To unfollow everyone use:
# Please Note that using this will remove all your followings
bot.unfollow_everyone()
Enter fullscreen mode Exit fullscreen mode

5/ Count number of followers of a user

We can get the number of followers of a user or our own using the get_user_followers() function. This will get the list of followers id.

from instabot import Bot

bot = Bot()
bot.login(username="your_username", password="your_password")

# Count number of followers
followers = bot.get_user_followers("username")
print("Total number of followers:")
print(len(followers))
Enter fullscreen mode Exit fullscreen mode

6/ Like and comment on a post

from instabot import Bot

bot = Bot()
bot.login(username="your_username", password="your_password")

post_link = "https://www.instagram.com/p/CWqleONFg4/"   # enter the post link
post = bot.get_media_id_from_link(post_link)

# like
bot.like(post)

# comment
bot.comment(post, "Comments ..")
Enter fullscreen mode Exit fullscreen mode

7/ Send messages to a user with send_message() function

from instabot import Bot

bot = Bot()
bot.login(username="your_username", password="your_password")

# To send message to a single user.
message = "Hello, Thanks for the gift"
bot.send_message(message, "username")

Enter fullscreen mode Exit fullscreen mode

GitHub link

More functions can be seen in the documentation here

Top comments (0)