您需要在中的类中添加一个计数器
__init__,然后在中添加一个计数器
on_status。然后,当计数器低于20时,它将在集合中插入一条记录。可以如下所示:
def __init__(self, api=None): super(StdOutListener, self).__init__() self.num_tweets = 0def 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


