简体   繁体   English

我想在 python 中循环一个机器人

[英]I want to loop a bot in python

the code of my bot written in python我的机器人代码写在 python

can someone tell me how to loop because I want to host on https://www.evennode.com/有人可以告诉我如何循环,因为我想在https://www.evennode.com/上主持

    #import modules
    import tweepy 
    import time
    
    auth = tweepy.OAuthHandler('','') #key
    auth.set_access_token('', '')  #token
    
    api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
    
    user = api.me()

    search = '#Evangelion'  #code
    numeroDeTweets = 40 #code
    
    for tweet in tweepy.Cursor(api.search, search).items(numeroDeTweets): #code
        try:
            print('') #code
            tweet.retweet() #code
            tweet.favorite() #code
            time.sleep(45) #code
        except tweepy.TweepError as e: #code
            print(e.reason) #code
        except StopIteration: #code
            break #code

Oh, Evangelion is mentioned here, good.哦,这里提到新世纪福音战士,好。 Really good.真的很好。 But jokes aside, as far as i understood you want to infinitely loop through twitter search result.但开个玩笑,据我所知,你想无限循环 twitter 搜索结果。 If so, there are two methods which will do it but with the key difference.如果是这样,有两种方法可以做到,但有关键的区别。

If you want to itereate through:如果你想迭代:


I. Every result possible before updating search queue I. 更新搜索队列之前的所有可能结果

If you wan to do so, you just have to remove numeroDeTweets variable from the items() argument and let it iterate:如果你想这样做,你只需要从items()参数中删除numeroDeTweets变量并让它迭代:

for tweet in tweepy.Cursor(api.search, search).items(): # No more limits!
    try:
        # your stuff here
    except:
        # and here

II.二。 First numeroDeTweets results and then search again首先numeroDeTweets结果,然后再次搜索

In that case you have to put for cycle into the while True loop to make it infinite and add a time.sleep() to add some cooldown.在这种情况下,您必须将for cycle 放入while True循环中以使其无限循环并添加time.sleep()以增加一些冷却时间。

So it will look something like that:所以它看起来像这样:

while True: # Doing it infinite amount of times
    for tweet in tweepy.Cursor(api.search, search).items(numeroDeTweets): #There ARE limits!
        try:
            # your stuff here
        except:
            # and here
        finally:
            time.sleep(45) # Taking rest for search to update

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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