【问题标题】:I want to loop a bot in python我想在 python 中循环一个机器人
【发布时间】:2021-03-13 17:58:48
【问题描述】:

我用 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 Automation Rules 并调整您的代码以删除喜欢的内容。你最好使用流式 API 并对新推文做出反应,而不是循环搜索 - 这样可以避免重复。

标签: python loops twitter bots host


【解决方案1】:

哦,这里提到了新世纪福音战士,很好。真的很好。但除了笑话,据我了解,您希望无限循环浏览 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

【讨论】:

  • man 这个很好,很详细,谢谢!
猜你喜欢
  • 2018-10-31
  • 2021-08-31
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
  • 1970-01-01
  • 2022-01-16
  • 2022-12-03
相关资源
最近更新 更多