Upload images to instagram using Python

Viewed 9298

I'm trying to do a simple Instagram python bot in order to upload images in my Instagram profile. I've already tried the most common libraries (InstagramAPI, instapy, insta-cly). While I was searching I found out that Instagram has changed something making those libraries useless.

Is there any library I can use? I know thatI can use Selenium in order to make it go but I'm wondering if there is any shortcut.

Trank you!

2 Answers

try this library: instabot https://pypi.org/project/instabot/

example of code for uploading an image:

from instabot import Bot

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

file = open('path_to_your_image', 'r')
bot.upload_photo(file, caption="your post caption")

Instabot python library allows you to upload images, stories, videos, make comments and much much more, but there are problems that are not resolved in other answers, so I wrote this code that takes care about all of that for you.

from instabot import Bot
import os
import shutil


def clean_up():
    dir = "config"
    remove_me = "imgs\img.jpg.REMOVE_ME"
    # checking whether config folder exists or not
    if os.path.exists(dir):
        try:
            # removing it because in 2021 it makes problems with new uploads
            shutil.rmtree(dir)
        except OSError as e:
            print("Error: %s - %s." % (e.filename, e.strerror))
    if os.path.exists(remove_me):
        src = os.path.realpath("imgs\img.jpg")
        os.rename(remove_me, src)


def upload_post():
    bot = Bot()

    bot.login(username="your_username", password="your_password")
    bot.upload_photo("imgs/img.jpg", caption="Caption for the post")


if __name__ == '__main__':
    clean_up()
    upload_post()
Related