Automate your Instagram Posts Using Python
Want to automate your Instagram posts? Then you have come to the right place, my friend. Impact-Site-Verification: eed0bba5–91ae-4e0f-bd0b-1aacc7c12879
A few days back I was scrolling through my Instagram feeds and was thinking of doing something new, something useful. All of a sudden I thought, why not do something fun with Instagram itself. Maybe, create a bot that can do stuff? So, I got on my laptop and started to check for some libraries…oh I forgot to mention that I am a person who loves automating stuff.
Coming back to the point, I saw that there are some libraries that I could import and use directly, but what's the fun in that!! And hence I started to think of something that most of the tutorials normally don’t show us about. The only thing that came to my mind was to automate Image posting on Instagram.
As per my personal view, Instagram posts get more engagement if I post images in the morning and evening. So, if I could make my bot post images with proper tags and descriptions at a specific time of the day then nothing like it.
Let’s Talk Tools:
I have used python as my language to code, selenium to navigate through Instagram, and pyautogui to for navigating through windows(for accessing the files from my local drive).
And yes, we have to download google chrome’s web driver also. Selenium uses a chrome driver to navigate through web apps. It can be downloaded from the below link :
https://chromedriver.chromium.org/downloads
Note: Download the version that matches your chrome browser's version.
The Process Flow :
For simplicity I will break down the whole process into smaller segments:
- Open Chrome
- Login to Instagram account
- Post the image
1. Open Chrome:
First, we need to download the chrome driver and keep it on a specific path. This path would be used by the code while opening the web browser.
But, before that let's get on with the imports.
Second, since Instagram is mainly meant to be an app, we can not directly publish our posts from our desktop/laptop. The trick is to emulate the browser as a mobile browser. I have described each line of the below code to help you understand it better:
2. Login to Instagram account:
The next step is to login using the username and password.
To provide the username, password and then click on the Login button, the bot needs to identify its elements and then take action. For this, we make use of xpaths to make the bot identify these elements. Below is the console screen from where we find out xpaths of each element that are present in the webpage.
To open the console:
a. Right-click anywhere in the browser window
b. Click on Inspect. Or you could use the shortcut Ctrl+Shift+I.
Throughout the code, I have used multiple wait statements to simulate human-like behavior and make sure that Instagram does not block our bot. The sleep(5) mentioned below is one wait statement that pauses the code for 5 seconds.
Once the bot has logged in it has to bypass the different popups that are displayed by Instagram. Below is one sample popup which is bypassed by the below popup.
3. Post the Image:
Now comes the main step i.e. to post the image. There are two steps that we need to follow for posting the image. First, upload the image, and second, add tags, description & post it.
Upload the image: We will create a function that would upload the image. The function would take the image path as an attribute. For uploading the image from our local disk we will use pyautogui library.
Add tags, description & post it: First, we will call the function upload_file_window_dialog_box and then add tags and post text description.
Don’t forget to close the driver once this is done. We do so using the driver.close() method.
And Voila!! It's done. You have successfully automated Instagram image posting.
Please try this out by yourself and do let me know your feedback in the comments. I will soon add more functionality to this bot and share it with you. So, keep an eye out for my posts.
Thank you for reading.