Growing your Instagram account is hard if you aren’t consistent in putting unique content on the table or you don’t have someone who looks after your PR(public relations).
Liking every photo on Instagram is a traditional way which we all have used some time or the other to bring attention to our account. It works, people will follow you and bots too, but if all you do in a day is to go to Instagram and like other people photos when you should be on the field clicking your best shots, these techniques fall apart. What if I teach you how to make a python bot who will like all the photos on your behalf and helps you straight out your Instagram priorities, does that makes you happy?
Do we have to install anything?
To make a bot which will
- Open the browser
- Goes to the Instagram login page
- Feeds your credential
- Opens your desired hashtag page
- Scroll the photos
- Start liking all the pictures one by one
you need to install two bindings, Selenium and Geckodriver.
Selenium is an open-source web-based automation tool. The Python APIs empower you to connect with the browser through Selenium. Selenium can send the standard Python commands to different browsers, despite variation in their browser’s design. To install Selenium on your machine go to command prompt and run
pip install selenium
It is a link between Selenium and your browser(Mozilla Firefox) which makes sure that bot is working smoothly in your browser. You have to download the installer, run it and save the address of the folder where you have installed it.
Firefox would use this driver to communicate to and fro. Since Firefox is an application which is running under OS, the operating system must have information about the directory where the driver is kept so that it can reach out to it when it is required.
This can be done by setting up a path. The list below explains how to set up a path.
- Go to My Computer and open the properties
- On the left sidebar, you will see ‘Advanced System Setting‘, click it
- Under Startup and Recovery block you will find Environment Variables, click it
- Under System Variables, select PATH and select the option to edit it.
- Copy paste the address of the directory where you have kept geckodriver
To make more simplistic for you I am posting some screenshots of the above process.
With this, the hard part is over and we can now jump up to the programming part. To understand the code I have divided into 4 blocks.
- Importing the required libraries
- Creating a class for this task and defining the constructors.
- The login function which will make the bot to login in Instagram.
- Function to Like photos
- Running the bot
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time
The selenium.webdriver module provides all the WebDriver implementations. Currently supported WebDriver implementations are Firefox, Chrome, IE and Remote. The Keys class provide keys in the keyboard like RETURN, F1, ALT etc.
How fast the web pages would load depends majorly on the internet connection you have, if it is slow we have to make our bot to wait for sometime before executing the next series of commands. Another fact which I need to press before you is Instagram doesn’t allow more than 200 likes in one hour, keeping that in mind we have to tune our bot too. For this, we use the time library.
class and constructors
class IGBot: def __init__(self, username, password): self.username=username self.password=password self.driver=webdriver.Firefox() def closeBrowser(self): self.driver.close()
__init__ is a constructor where the program will come whenever the new object/instance of the class would be created. self in it represents the object which has username and password variables in it. we can initialise the variables as per our needs here. This comes handy if you don’t want anyone later to change the values of variables later in the program.
That means when you will create the object of this class you will pass the username and password for the Instagram account for which you want the bot to work.
At the end of the session, you would want the bot to exit your Instagram account and for that closeBrowser() will be brought to use.
def login(self): driver=self.driver driver.get("https://www.instagram.com/") time.sleep(7)
We will pass the object of the class to the login method. Next, the instance of WebDriver is to be created and then use it to open the Instagram. self is an object of the simple class it is not capable of opening the browser and a website in it, driver is an object of Webdriver which will help us do that. I am making the bot to wait for 7 seconds so that no matter how poor the internet connection might be it will wait till the website fully opens. You can change the time as per your needs.
When asked to open the Instagram, our bot would open the above page which is more of a sign-up page for the Instagram. It’s our duty to tell bot where the log-in is and how to find it every time when it lands on this page.
It is nothing but a web page whose front end source code we can see by inspecting it. To inspect any web page just right click and select Inspect Element.
As soon as you Inspect Element, a small window will pop. You have to select login to find out what code is used to place it.
There are many forms in which this code can be taken and used. In this post, I will demonstrate X-Path coz it is super fast.
login_button=driver.find_element_by_xpath("/html/body/span/sect ion/main/article/div/div/p/a") login_button.click() time.sleep(2)
In the method find_element_by_xpath() copying the XPath will make the bot to locate the login in the page. Once located now we can click it go to the login page. Here too I am delaying it for 2 seconds.
user_name_element=driver.find_element_by_xpath("//input[@name=' username']") user_name_element.clear() user_name_element.send_keys(self.username) password_element=driver.find_element_by_xpath("//input[@name='p assword']") password_element.clear() password_element.send_keys(self.password) login_enter_button=driver.find_element_by_xpath("/html/body/span/section/main/div/article/div/div/div/form/span/button") login_enter_button.click() time.sleep(2)
Once we reach on to the login page, now we will repeat the process of finding the XPath of username, password and Login-enter(button). The above code does 3 things
- Locate username, wipe out anything written in it and feed the username which we have passed to the constructor at the very beginning.
- Locate password, wipe out anything written in it and feed the password which we have passed to the constructor at the very beginning.
- Locate the login button and click it.
like photos method
def like_photo(self, hashtag): driver=self.driver driver.get("https://www.instagram.com/explore/tags/"+ hashtag+ "/") time.sleep(2)
like_photo() method would be provided with the object and the hashtag at the time of calling. A new object of WebDriver will open the page.
Note- “https://www.instagram.com/explore/tags/” has top be placed manually by you.
scheight = .1 while scheight < 9.9: driver.execute_script("window.scrollTo(0, document.body.scrollHeight/%s);" % scheight) scheight += .01
Instagram doesn’t load all the images by itself, you have to scroll down to see the new images. So our bot needs to do that too. Above code scrolls the entire page to which Instagram will load all the new pics.
pic_href= hrefs=driver.find_element_by_tag_name('a') elems = driver.find_elements_by_xpath("//a[@href]") pic_hrefs=[elem.get_attribute('href') for elem in hrefs] [pic_hrefs.append(href) for href in hrefs_in_view if href not in pic_hrefs]
You can’t like all the photos at once and nor does your bot. Instagram makes sure that before liking any photos you have to open it. Above code will locate all the photos currently residing on the page and store the XPath of each photo in a list.
for pic_href in pic_hrefs: driver.get(pic_href) driver.execute_script("window.scrollTo(0, document.body.scrollHeight);"); try: driver.find_element_by_link_text("Like").click() time.sleep(18) except Exception as e: time.sleep(2)
With this, we come to the most exciting part of our code. Each photo will be open from the list we have just collected, it would be scrolled totally and then by locating the like button(you have to enter all the XPath manually)
Run the bot
IG=IGBot("your username","your password") IG.login() IG.like_photo("hashtag which you want to open")
I hope you find it exciting enough to try it for yourself. Please let me know in comments how you felt about the post.