简体   繁体   中英

How can I make the bot repeat the loop?

I am running an Instagram test bot as a project for school.

However, I try to add some sleep time between each action (follow+like).

The bot sleeps after the first action, and then stop working.


import argparse
import os
import sys
import time
from tqdm import tqdm
sys.path.append(os.path.join(sys.path[0], '../'))
from instabot import Bot
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument('-u', type=str, help="username")
parser.add_argument('-p', type=str, help="password")
parser.add_argument('-proxy', type=str, help="proxy")
parser.add_argument('users', type=str, nargs='+', help='users')
args = parser.parse_args()
bot = Bot()
bot.login(username=args.u, password=args.p,
          proxy=args.proxy)
for username in args.users:
   medias = bot.get_user_medias(username, filtration=False)
if len(medias):
        likers = bot.get_media_likers(medias[0])

for liker in tqdm(likers):
           bot.like_user(liker, amount=1)  
          followed= bot.follow(liker)  
          while(follow): 
            time.sleep(60) 

I'd like to be able to keep the bot running also after it sleeps for the first time.

Any suggestions?

I do not have instagram so I can not test it out but what I see you have few issues in your code.

First one is that you get medias just for last user. It might be fixed by:

medias = []
for username in args.users:
   medias += bot.get_user_medias(username, filtration=False)

Then similar think is with likers:

likers = []
for media in medias:
    likers += bot.get_media_likers(media)

And then to you actual question. You used sleep after like and foloww, not in between. And you have it in loop from which you never break. So if you want to wait in after like and follow then do this:

for liker in tqdm(likers):
    bot.like_user(liker, amount=1)
    followed = bot.follow(liker)
    time.sleep(60) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM