使用instabot遇AttributeError及429错误:Bot无set_proxy属性求助
解决Instabot的两个问题:set_proxy属性不存在和429请求限流
1. 修复AttributeError: 'Bot' object has no attribute 'set_proxy'
Instabot的Bot类并没有set_proxy方法,你需要通过以下两种方式之一设置代理:
方式一:初始化Bot时直接传入代理参数
将代理配置作为参数传递给Bot()构造函数:
def unfollow_users(username, password, users_to_unfollow, unfollow_interval=60, proxy=None): # 初始化Bot时传入proxy bot = Bot(proxy=proxy) bot.login(username=username, password=password) # ... 后续代码不变
方式二:手动设置API会话的代理
如果你的代理是字典格式(如代码中的proxy_config),可以直接赋值给bot.api.proxies:
def unfollow_users(username, password, users_to_unfollow, unfollow_interval=60, proxy=None): bot = Bot() if proxy: # 直接设置API的代理配置 bot.api.proxies = proxy bot.login(username=username, password=password) # ... 后续代码不变
2. 修复429请求限流错误
429错误是Instagram触发了速率限制,针对批量取消关注操作,你需要优化操作逻辑来规避检测:
- 使用随机延迟替代固定间隔:固定间隔会被识别为机器人行为,改为60-120秒的随机延迟
- 减少单次操作的用户数量:分批次处理用户列表,避免短时间内大量请求
- 添加人性化前置操作:登录后模拟浏览行为,降低被检测风险
修改后的完整代码示例:
from instabot import Bot import time import random def unfollow_users(username, password, users_to_unfollow, unfollow_interval_min=60, unfollow_interval_max=120, proxy=None): bot = Bot() if proxy: bot.api.proxies = proxy bot.login(username=username, password=password) # 登录后添加随机延迟,模拟浏览行为 time.sleep(random.randint(5, 10)) bot.get_feed() time.sleep(random.randint(3, 7)) for user_to_unfollow in users_to_unfollow: user_id = bot.get_user_id_from_username(user_to_unfollow) if user_id: bot.unfollow(user_id) print(f"Unfollowed {user_to_unfollow}") # 使用随机延迟 delay = random.randint(unfollow_interval_min, unfollow_interval_max) time.sleep(delay) else: print(f"User {user_to_unfollow} not found") bot.logout() if __name__ == "__main__": instagram_username = 'usernm' instagram_password = 'pass' users_to_unfollow = ["user1id", "user2id", "user3id"] # 代理配置保持不变 proxy_config = { 'http': 'http://212.193.137.206:62740:NReJKktP:vjTr2QJC', 'https': 'https://185.81.71.172:63310:NReJKktP:vjTr2QJC', } unfollow_users(instagram_username, instagram_password, users_to_unfollow, proxy=proxy_config)
额外提示:
- 避免在短时间内多次登录同一账号,Instagram会临时限制登录
- 如果仍触发限流,进一步延长延迟时间(比如120-180秒),并减少每次处理的用户数量
内容的提问来源于stack exchange,提问作者tukhsanov
相关产品推荐
相关产品推荐

