Tweepy 流式传输 API:过滤用户

Tweepy streaming API: filtering on user

我正在尝试使用 tweepy(和 python 3)简单地连接到 Twitter streaming API,并流式传输来自给定单个用户的所有推文。

我觉得这是可能的,所以我有以下简单的代码来做到这一点:

from tweepy import StreamListener
from tweepy import Stream
import tweepy

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

class StdOutListener(StreamListener):

    def on_data(self, data):
        # process stream data here
        print(data)

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

if __name__ == '__main__':
    listener = StdOutListener()
    twitterStream = Stream(auth, listener)
    twitterStream.filter(follow=['575930104'])

当我从命令行 运行 执行此操作时,我只是从 Twitter 取回了一堆 406 代码。我尝试使用 tweepy 的方式有什么非常明显的错误吗,或者 "follow" 参数没有按照我的想法设计?

编辑: 我还在 tweepy 讨论板上发帖 this,仅供参考。

我能够使用 Tweepy 3.2.0 在 Python 3.4 上重现您的问题。升级到 3.3.0 解决了问题:

$ bin/pip install -U tweepy==3.2.0
[...]
Successfully installed tweepy requests-oauthlib six requests
Cleaning up...
$ bin/python test.py 
406
^CTraceback (most recent call last):
[...]
KeyboardInterrupt
$ bin/pip install -U tweepy==3.3.0
[...]
Successfully installed tweepy requests requests-oauthlib six
Cleaning up...
$ bin/python test.py
{"created_at":"Fri Feb 27 14:02:02 +0000 2015","id":571309283217768448,"id_str":"571309283217768448",
[...]

Tweepy 3.3.0 Changelog 提到了几个流媒体改进,虽然我没有看到他们提到的 'freezing' 问题。