小编Abi*_*bin的帖子

一分钟后无法在tweety中停止流式传输

我试图使用Stream.filter()方法将Twitter数据流式传输一段时间,比如5分钟.我将检索到的推文存储在JSON文件中.问题是我无法从程序中停止filter()方法.我需要手动停止执行.我尝试使用时间包根据系统时间停止数据.我能够停止向JSON文件编写推文,但是流方法仍在继续,但它无法继续下一行代码.我正在使用IPython笔记本来编写和执行代码.这是代码:

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)

from tweepy import Stream
from tweepy.streaming import StreamListener

class MyListener(StreamListener):

    def __init__(self, start_time, time_limit=60):
        self.time = start_time
        self.limit = time_limit

    def on_data(self, data):
        while (time.time() - self.time) < self.limit:
            try:
                saveFile = open('abcd.json', 'a')
                saveFile.write(data)
                saveFile.write('\n')
                saveFile.close()
                return True
            except BaseException as e:
                print 'failed ondata,', str(e)
                time.sleep(5)
        return True

    def on_status(self, status):
        if (time.time() - self.time) >= self.limit:
            print 'time is over'
            return false

    def on_error(self, status):
        if (time.time() …
Run Code Online (Sandbox Code Playgroud)

python twitter tweepy

7
推荐指数
1
解决办法
8345
查看次数

标签 统计

python ×1

tweepy ×1

twitter ×1