Education

Make Instagram Bot in Python and Gain Followers.

Pinterest LinkedIn Tumblr

Instagram is most famous social media of current time with almost daily 500 million active users worldwide. Everyone wants to get famous in Instagram and get some followers. But Instagram’s algorithm is not that much friendly. You need to be very much active in Instagram to gain followers but honestly not every one has that much time.

You get more followers, like and comment on your posts when you do comments and give likes to others. But as said earlier, you are too busy to do that. So wouldn’t it be great if someone could help in this? That someone is your true friend Python. Yes, we can use Python to build an Instagram Bot that will comment or like or do both.

Before starting I assume that you guys have a little bit knowledge of Python and knows about pip. Now without wasting much time let’s dive into script making.

First of all we need to install InstaPy using pip.

pip install InstaPy

InstaPy is tool made by Tim Grossmann for making Instagram interaction more efficient. You can read more about InstaPy here.

Now once you have installed InstaPy, we can go on to the coding section. Open any of the your favorite code editor and write below code.

from instapy import InstaPy

Read Also: A Learning Resource Catalog For All.

This will load InstaPy class required for making Instagram Bot.

from instapy import InstaPy

session = InstaPy(username="<username>", password="<password>")

session.login()

Just change the <username> with your username and <password> with you Instagram password. After that run your script and check the console.



Your bot will do some checks before logging, these include checking if Instagram server is down or not, making a session so that you don’t need to login again and again. All of these action can be seen in your console or in browser itself.

INFO [2020-06-01 18:59:37] [nutt.kracker]  Session started!
INFO [2020-06-01 18:59:37] [nutt.kracker]  -- Connection Checklist [1/3] (Internet Connection Status)
INFO [2020-06-01 18:59:41] [nutt.kracker]  - Internet Connection Status: ok
INFO [2020-06-01 18:59:41] [nutt.kracker]  - Current IP is "106.211.90.84" and it's from "India/IN"
INFO [2020-06-01 18:59:41] [nutt.kracker]  -- Connection Checklist [2/3] (Instagram Server Status)
INFO [2020-06-01 18:59:51] [nutt.kracker]  - Instagram WebSite Status: Currently Up 
INFO [2020-06-01 18:59:51] [nutt.kracker]  - Instagram Response Time: 130.549 ms 
INFO [2020-06-01 18:59:51] [nutt.kracker]  - Instagram Reponse Code: 200
INFO [2020-06-01 18:59:51] [nutt.kracker]  - Instagram Server Status: ok
INFO [2020-06-01 19:01:03] [nutt.kracker]  Logged in successfully!

Check last line of the log. Yes, you’ve successfully logged into your account using Python. Isn’t it amazing? You have wrote just three lines of code and you have made a Instagram. But hold on, that is not what we wanted. We want to comment and likes post using this post. So, let’s move further.

Read Also: Quick Dive In Python’s NumPy for Data Science.

Go back to your editor and add following code:-

from instapy import InstaPy
session = InstaPy(username="<your_username>", password="<your_password>")

session.login()

session.like_by_tags(["technology", "techhacks"], amount=10)

We are giving like_by_tags a list of tags that we want to like. Change them according to your targets. amount is the number of the posts you want to like. Please note apart from the amount given by you, InstaPy will also like 9 top posts of that same hashtags.

INFO [2020-06-01 20:16:11] [nutt.kracker]  --> b'technology'
INFO [2020-06-01 20:18:39] [nutt.kracker]  desired amount: 19  |  top posts [disabled]: 9  |  possible posts: 14168842
INFO [2020-06-01 20:18:46] [nutt.kracker]  Like# [1/19]
INFO [2020-06-01 20:18:46] [nutt.kracker]  https://www.instagram.com/p/CA5XSFanOBE/
INFO [2020-06-01 20:19:32] [nutt.kracker]  Image from: b'sayed.morteza.ahmadi'
INFO [2020-06-01 20:19:32] [nutt.kracker]  Link: b'https://www.instagram.com/p/CA5XSFanOBE/'
INFO [2020-06-01 20:19:32] [nutt.kracker]  Description: b'#diyelectronics #doityourself #diy #iron #soldering #pcba #smd #smt #pcb #microcontroller #arduino #electroniclovers #internetofthings #epaper #electronics #engineering\n#tech #technology #makersofinstagram #makerspace #howto #car #love #style'

As you can see the desired amount here is 19 rather than 10, because Instagram Bot will like 9 top posts by default. If you needed just that then you Instagram Bot is ready but wait, there is a lot more you can do will this Instagram Bot.



Liking posts using InstaPy is good, how about commenting? Well, we can do that as well just by adding couple more lines.

from instapy import InstaPy 

session = InstaPy(username="<your_username>", password="<your_password>") 
session.login() 
session.like_by_tags(["technology", "techhacks"], amount=10)
session.set_do_comment(True, percentage=50)
session.set_comments(["Nice!", "Sweet!", "Beautiful :heart_eyes:"])

set_do_comment() method enables the commenting process. Percentage parameter indicates on how many posts do you want to post a comment. If there are 10 posts to be liked than Instagram Bot will comment on 5 posts. Bot will choose that posts randomly.

Read Also: Top 5 Bug Fixing Tools For Python Developers!

Once you are done playing with your Instagram Bot, it’s a good practice to close the session.

from instapy import InstaPy 

session = InstaPy(username="<your_username>", password="<your_password>") 
session.login() 
session.like_by_tags(["technology", "techhacks"], amount=10) 
session.set_do_comment(True, percentage=50) 
session.set_comments(["Nice!", "Sweet!", "Beautiful :heart_eyes:"])

session.close()

Some Bonus Tips.

Maximum Followers Limit

If you are a growing page and using Instagram Bot to get some followers then it is useless to interact with accounts having high number of followers. In that case you can set max followers limit with which your bot will interact.

session.set_relationship_bounds(enabled=True, max_followers=5000)

Headless Browser

If you don’t want to see browser running but still want your Instagram Bot to do desire task then you can use headless browser option. By using this feature your Bot won’t be running in browser but instead it will run only on console.

session = InstaPy(username='<username>', password='<password>', headless_browser=True)

You can do a lot more with InstaPy. For complete documentation check this link. If you get stuck or getting an error then DM me on our Instagram Page.

I hope you guys have enjoyed this as much as I have enjoyed writing this. Do follow us on our official Instagram Page. We posts more awesome like this everyday.

Author

Comments are closed.