繁体   English   中英

我想在 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 搜索结果。 如果是这样,有两种方法可以做到,但有关键的区别。

如果你想迭代:


I. 更新搜索队列之前的所有可能结果

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

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

二。 首先numeroDeTweets结果,然后再次搜索

在这种情况下,您必须将for cycle 放入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

暂无
暂无

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

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