【问题标题】:How to close twitter stream如何关闭推特流
【发布时间】:2013-03-26 05:19:57
【问题描述】:

我在下面包含了与 Tweepy 一起使用的代码,Tweepy 是 Python 的 Twitter API 库。虽然我正在尝试我在网上找到的大多数方法,但他们未能关闭连接或停止流。有什么办法吗?

在我的函数内部

 setTerms = s.split(',')
 streaming_api = tweepy.Stream(auth=auth, listener=StreamListener(), timeout=60 )
    if (s == '0'):
        streaming_api.disconnect()
        raise web.seeother('/dc')
        print "Failed to see this"
    try:
        twt = streaming_api.filter(track=setTerms)
    except:
        streaming_api.disconnect()
        #also cannot see this
    raise web.seeother('/stream')

这里是流监听类

class StreamListener(tweepy.StreamListener):
        def on_status(self, status):
            try:
                printer(status.text, status.created_at)
            except Exception, e:
                pass
        def on_error(self, status_code): 
            print >> sys.stderr, 'Encountered error with status code:', status_code
            return True
        def on_timeout(self): 
            print >> sys.stderr, 'Timeout...'
            return True 

【问题讨论】:

    标签: python twitter python-2.7 tweepy


    【解决方案1】:

    第一次调用stream.disconnect()(在if (s == '0'): 内)时,您还没有调用filter,因此永远不会连接流。假设您使用的是最新版本的tweepy,其余代码应该是正确的。请注意,except 块几乎不会被调用,因为流运行时发生的任何错误都会传递给on_error 回调。

    【讨论】:

      猜你喜欢
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      • 2011-01-20
      • 2016-04-07
      • 2022-08-24
      相关资源
      最近更新 更多