Tweepy Streaming - Stop collecting tweets at x amount

前端 未结 1 341
一生所求
一生所求 2020-12-09 23:29

I\'m looking to have the Tweepy Streaming API stop pulling in tweets after I have stored x # of tweets in MongoDB.

I have tried IF and WHILE statements inside the c

相关标签:
1条回答
  • 2020-12-09 23:58

    You need to add a counter inside of your class in __init__, and then increment it inside of on_status. Then when the counter is below 20 it will insert a record into the collection. This could be done as show below:

    def __init__(self, api=None):
        super(StdOutListener, self).__init__()
        self.num_tweets = 0
    
    def on_status(self, status):
        record = {'Text': status.text, 'Created At': status.created_at}
        print record #See Tweepy documentation to learn how to access other fields
        self.num_tweets += 1
        if self.num_tweets < 20:
            collection.insert(record)
            return True
        else:
            return False
    
    0 讨论(0)
提交回复
热议问题