推文流:如何设置推文数限制?

时间:2016-08-03 22:21:46

标签: python twitter

我使用python脚本流式传输推文并使用tweepy将它们存储在Mongodb数据库中。一切都工作正常,但当我试图设置推文数量的限制时,不再检索推文,我看不到错误,你能帮忙吗?

class MyListener(StreamListener):

  def __init__(self, num_tweets, max_tweets=60):
        self.numTweets = num_tweets
        self.limit = max_tweets

def on_data(self, data):
    print("tweet")
     while self.numTweets < self.limit:
        #self.numTweets+=1
        try:
            client = MongoClient()
            db = client['twitter1_db']
            collection = db['twitter1_collection']
            tweet = json.loads(data)
            collection.insert_one(tweet)
            print(tweet)
            self.numTweets= self.numTweets+ 1
            return True
        except BaseException as e:
            print("Error on_data: %s" % str(e))
        return True

def on_error(self, status):
    print(status)`

twitter_stream = Stream(auth, MyListener(num_tweets, max_tweets)) twitter_stream.filter(track=Keywords_list)

0 个答案:

没有答案