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

I want to loop a bot in python

我的机器人代码写在python

谁能告诉我如何循环,因为我想在 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

哦,这里提到新世纪福音战士,不错。真的很好。但开个玩笑,据我所知,你想无限循环 Twitter 搜索结果。如果是这样,有两种方法可以做到,但有关键的区别。

如果你想遍历:


我。更新搜索队列之前可能的每个结果

如果你想这样做,你只需要从 items() 参数中删除 numeroDeTweets 变量并让它迭代:

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

二.先 numeroDeTweets 个结果然后再搜索

在这种情况下,你必须将 for 循环放入 while True 循环中以使其无限循环并添加 time.sleep() 以增加一些冷却时间。

所以它看起来像这样:

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