我有一个Twitter机器人,可以响应包含主题标签的推文。我一直在使用twt = api.search(q='param')来发布推文,并且一直运行良好,但是此后我切换到了myStreamListener = MyStreamListener()twt = tweepy.Stream(auth = api.auth, listener=myStreamListener())来实时发布推文。这是行不通的,我也不知道为什么。这是我的代码:

myStreamListener = MyStreamListener()
twt = tweepy.Stream(auth = api.auth, listener=myStreamListener())

twt.filter(track=['#www'], async=True)

#list of specific strings we want to omit from responses
badWords = ['xxx','yyy' ]

#list of specific strings I want to check for in tweets and reply to

genericparam = ['@zzz']

def does_contain_words(tweet, wordsToCheck):
    for word in wordsToCheck:
        if word in tweet:
            return True
    return False

for currentTweet in twt:
    #if the tweet contains a good word and doesn't contain a bad word
    if does_contain_words(currentTweet.text, genericparam) and not does_contain_words(currentTweet.text, badWords):
        #reply to tweet
        screen = currentTweet.user.screen_name
        message = "@%s this is a reply" % (screen)
        currentTweet = api.update_status(message, currentTweet.id)

最佳答案

我相信您的问题是您正在尝试通过流发送答复,而不是在单独的线程中使用http请求。不可能按照以下说明进行操作:

https://dev.twitter.com/streaming/overview

07-24 13:18