如何使用Python和 InstaPy 制作 Instagram 机器人?
在本文中,我们将了解如何使用Python和 InstaPy 制作 Instagram 机器人。
如今,机器人在发送消息、上传照片、发送愿望等方面非常普遍。机器人减少了我们的工作,节省了时间。今天我们正在创建一个 Instagram 机器人,它可以做以下事情。
机器人执行的功能
- 关注一位或多位朋友。
- 取消关注一个或一组人员。
- 取消关注所有人。
- 计算任何用户的关注者数量。
- 向关注者或关注者列表发送消息。
- 在聊天中发送喜欢。
- 发布照片。
Instabot 库:它是 Instagram 的推广脚本和 API Python包装器。
pip install instabot
登录
在执行任何 login() 函数之前,我们需要先导入 instabot 库并登录。
Python3
# Import instabot library
from instabot import Bot
# Create a variable bot.
bot = Bot()
# Login
bot.login(username="your_userid",
password="your_password")
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Follow
# To follow single person.
bot.follow("geeks_for_geeks")
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Follow
# To follow more person.
list_of_user = ["user_id1", "user_id2", "user_id3", "...."]
bot.follow_users(list_of_user)
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# To unfollow a single person.
bot.unfollow("geeks_for_geeks")
Python3
from instabot import Bot
bot = Bot()
bot.login(username = "your_username",
password = "your_password")
# To unfollow more person.
unfollow_list = ["user_id1", "user_id2", "user_id3", "..."]
bot.unfollow_users(unfollow_list)
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Unfollow everyone!
# To unfollow everyone use:
# Please use this part very carefully.
bot.unfollow_everyone()
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Count number of followers
followers = bot.get_user_followers("geeks_for_geeks")
print("Total number of followers:")
print(len(followers))
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Message
# To send message to a single person.
message = "I like GFG"
bot.send_message(message, "geeks_for_geeks")
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Message
# To send same message to many follower.
message = "I like GFG"
list_of_userid = ["user_id1", "user_id2", "user_id3", "..."]
bot.send_messages(message, list_of_userid)
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Send like in messages
# To send like to one or more person.
send_like_list = ["user_id1", "user_id2", "user_id3", "..."]
bot.send_like(send_like_list)
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Post photos
# Photos need be resized and, if not in ratio given below.
# jpg format works more better than others formats.
# Acceptable Ratio of image:- 90:47, 4:5, 1:1(square image).
# Keep image and program in same folder.
# -----------------------------------------------------------
bot.upload_photo("filename.jpg", caption="Write caption here.")
输出:
跟随
要关注一位朋友,我们可以使用 follow()函数。
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Follow
# To follow single person.
bot.follow("geeks_for_geeks")
输出:
要关注许多用户,我们需要先列出用户名,然后使用“follow_users”函数进行关注。
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Follow
# To follow more person.
list_of_user = ["user_id1", "user_id2", "user_id3", "...."]
bot.follow_users(list_of_user)
取消关注
要取消关注一个人,我们将使用 unfollow()函数。
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# To unfollow a single person.
bot.unfollow("geeks_for_geeks")
输出:
要取消关注许多人,请创建一个取消关注列表,然后使用“unfollow_users”函数。
Python3
from instabot import Bot
bot = Bot()
bot.login(username = "your_username",
password = "your_password")
# To unfollow more person.
unfollow_list = ["user_id1", "user_id2", "user_id3", "..."]
bot.unfollow_users(unfollow_list)
取消关注所有人
在这里,我们将使用 unfollow_everyone()函数取消关注所有人进入我们的帐户。
警告:请仅在您确实想取消关注所有人时才使用这部分代码。
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Unfollow everyone!
# To unfollow everyone use:
# Please use this part very carefully.
bot.unfollow_everyone()
计算关注者数量
我们可以使用“get_user_followers”函数检查我们自己的关注者或任何关注者的数量。这个函数创建一个追随者 id 的列表。
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Count number of followers
followers = bot.get_user_followers("geeks_for_geeks")
print("Total number of followers:")
print(len(followers))
输出:
发送信息
使用 send_message()函数向一个人发送消息很简单。
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Message
# To send message to a single person.
message = "I like GFG"
bot.send_message(message, "geeks_for_geeks")
输出:
向许多人发送相同的信息。
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Message
# To send same message to many follower.
message = "I like GFG"
list_of_userid = ["user_id1", "user_id2", "user_id3", "..."]
bot.send_messages(message, list_of_userid)
发送喜欢的消息
要发送喜欢的用户列表,然后使用“send_like”函数。机器人根据聊天中的列表向朋友发送赞。
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Send like in messages
# To send like to one or more person.
send_like_list = ["user_id1", "user_id2", "user_id3", "..."]
bot.send_like(send_like_list)
输出:
发布照片
要在 Instagram 上发布照片,我们需要检查照片是否符合给定的比例。如果照片不是给定的比例,我们需要调整它的大小。最简单的比例是1:1。
Python3
from instabot import Bot
bot = Bot()
bot.login(username="your_username",
password="your_password")
# Post photos
# Photos need be resized and, if not in ratio given below.
# jpg format works more better than others formats.
# Acceptable Ratio of image:- 90:47, 4:5, 1:1(square image).
# Keep image and program in same folder.
# -----------------------------------------------------------
bot.upload_photo("filename.jpg", caption="Write caption here.")
输出: