Unable to stop Streaming in tweepy after one minute

后端 未结 3 534
逝去的感伤
逝去的感伤 2021-02-03 12:26

I am trying to stream twitter data for a period of time of say 5 minutes, using the Stream.filter() method. I am storing the retrieved tweets in a JSON file. The problem is I am

3条回答
  •  北海茫月
    2021-02-03 12:55

    1. In order to close the stream you need to return False from on_data(), or on_status().

    2. Because tweepy.Stream() runs a while loop itself, you don't need the while loop in on_data().

    3. When initializing MyListener, you didn't call the parent's class __init__ method, so it wasn't initialized properly.

    So for what you're trying to do, the code should be something like:

    class MyStreamListener(tweepy.StreamListener):
        def __init__(self, time_limit=60):
            self.start_time = time.time()
            self.limit = time_limit
            self.saveFile = open('abcd.json', 'a')
            super(MyStreamListener, self).__init__()
    
        def on_data(self, data):
            if (time.time() - self.start_time) < self.limit:
                self.saveFile.write(data)
                self.saveFile.write('\n')
                return True
            else:
                self.saveFile.close()
                return False
    
    myStream = tweepy.Stream(auth=api.auth, listener=MyStreamListener(time_limit=20))
    myStream.filter(track=['test'])
    

提交回复
热议问题