Tweepy 流 API:按用户过滤

10

我正在尝试使用tweepy(和Python 3)简单连接到Twitter流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(使用pip show tweepy)?我是用3.3.0进行测试的。 - Martijn Pieters
我目前使用的是版本3.2.0和Python 3.4。也许我应该升级到3.3.0。让我试一下,看看会发生什么。 - gogurt
@MartijnPieters 我刚刚升级到3.3.0,现在它运行良好。我认为这可能是3.2.0中的一个错误。可能与这里讨论的错误有关?http://discuss.tweepy.org/t/filtering-hangs-in-python-3/194感谢您指出有一个新版本...我没有看到。如果你把它变成答案,我会接受的。 - gogurt
1
啊,诀窍是尝试使用Python 3.4而不是2.7。在3.2.0中重现,在3.3.0中修复。 - Martijn Pieters
1个回答

7
我能够在使用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 更新日志 中提到了几项流媒体改进,尽管我没有看到他们提到的“冻结”问题。


3
在Tweppy 3.3.0中,如果我使用stream.filter(follow=['@atm_informa']),会收到406响应。但是,如果我使用stream.filter(follow=['575930104']),一切正常。代码和用户名之间有什么关系?谢谢。 - franco_b
@franco_b:抱歉,我不知道。 - Martijn Pieters
3
http://gettwitterid.com/?user_name=atm_informa&submit=GET+USER+ID 为用户名称和用户 ID 之间的关系提供了链接。 - franco_b

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接