使用Python将图像上载到instagram

2024-10-04 01:33:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试做一个简单的Instagram python机器人,以便在我的Instagram个人资料中上传图像。我已经尝试过最常见的库(InstagramAPI、instapy、insta-cly)。当我搜索的时候,我发现Instagram已经改变了一些东西,使那些库变得无用

有我可以使用的图书馆吗?我知道我可以使用硒,以使它去,但我想知道是否有任何捷径

谢谢你


Tags: 图像图书馆机器人instagram捷径insta个人资料instagramapi
2条回答

尝试此库:instabothttps://pypi.org/project/instabot/

上载图像的代码示例:

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库允许您上传图像、故事、视频、发表评论等,但还有一些问题在其他答案中无法解决,因此我编写了这段代码,为您解决所有这些问题

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()

相关问题 更多 >