How to post on Instagram using Python

 How to post on Instagram using Python

You will learn about how to post on Instagram using Python. We will do this very easily and you will also be able to give caption to your post.




Logic and Explanation

For posting pictures on Instagram using python, we will use a module instabot. As it is a module first thing we have to do is to install it on our computer using pip.

pip install instabot

Now we will import the bot from instabot which will actually send the post on our profile.

from instabot.bot.bot import Bot

After doing this we will create a instance of this bot in our python program.

bot = Bot()

Now, the next thing we have to do is to login in our instagram account using bot. For this we will write the below command. Here, replace your username here with your username and password here with the password of the respective id.

bot.login(username = "Your username here", password = "Password here")

That's almost done, now we will post a photo on instagram. For this we will write the below line and will provide the complete path of the pic on your computer and caption for the post.

bot.upload_photo("Path of Photo",caption="Give caption to your post")

This program will work when you will run it for the first time. But on running second time it will create problem because when we log in with our instagram id in the bot it is saved in pycache. So, we have to clear cache from the bot. For this we will write the below command.

import os
try:
    os.remove("config/<Your username here>_uuid_and_cookie.json")
except:
    pass

I hope the code will be clear to you. Here is the complete source code for the process. Note that you have to replace 

<Your username here> with the username of your instagram account and password with it's password at their respective places in the code.

Source Code

from instabot.bot.bot import Bot
import os
bot = Bot()

try:
    os.remove("config/<Your username here>_uuid_and_cookie.json")
except:
    pass
bot.login(username = "Your username here", password = "Password here")
bot.upload_photo("Path of Photo",caption="Give caption to your post")

If you like the content, share it in your social media platforms so that we can help the maximum. If you have any query then feel free to ask in the comments below.

Check Similar Programs Here - Python Programs - Question and Answers


Enjoy Coding!!!


For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post