如何在 tweepy 的流 API 中使用“计数"参数? [英] How to use the 'count' parameter in tweepy's streaming API?
问题描述
当我在 filter()
调用中不包含 count
参数时,我可以很好地使用流 API,但是当我尝试指定来自我想要接收的历史记录,我的流对象返回 None
.
导入tweepy从 tweepy.streaming 导入 StreamListener, Stream类监听器(StreamListener):def on_status(self, status):打印'-' * 20打印状态文本返回def get_tweets(请求):# 如果 request.is_ajax():# 所有密钥和秘密都在此处声明,但出于安全原因被删除.auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)api = tweepy.API(auth)听众=听众()流 = 流(身份验证,侦听器)流过滤器(跟随=(14739093",),计数=-5)
我也尝试了以下方法,看看它返回了什么.
<预><代码>>>>something = stream.filter(follow=("14739093",), count=-5)>>>打印一些东西没有任何感谢您的帮助!
Stream.filter
总是返回 None
,它的工作只是将数据传递给 StreamListener
.
您的问题是 Twitter 只允许某些角色"使用 count
参数.
对捕获所有状态感兴趣的 Firehose、Links、Birddog 和 Shadow 客户端应保持对每秒收到的状态数量的当前估计,并注意收到最后一个状态的时间.重新连接后,客户端可以估计要请求的适当积压.请注意,count 参数不允许在其他地方使用,包括 track、sample 和默认访问角色.
这就是您在尝试使用 count
参数时收到 413
错误的原因——您处于默认访问"角色.
I can use the streaming API just fine when I don't include the count
parameter in filter()
call, but when I try to specify how many tweets from my history I want to receive, my stream object returns None
.
import tweepy
from tweepy.streaming import StreamListener, Stream
class Listener (StreamListener):
def on_status(self, status):
print '-' * 20
print status.text
return
def get_tweets(request):
# if request.is_ajax():
# All keys and secrets are declared here, but were removed for security reasons.
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
listener = Listener()
stream = Stream(auth, listener)
stream.filter(follow=("14739093",), count=-5)
I also tried the following, to see what it was returning.
>>> something = stream.filter(follow=("14739093",), count=-5)
>>> print something
None
Thanks for your help!
Stream.filter
always returns None
, its job is just to pass the data on to the StreamListener
.
Your problem is that Twitter only allows the count
parameter for certain "roles".
Firehose, Links, Birddog and Shadow clients interested in capturing all statuses should maintain a current estimate of the number of statuses received per second and note the time that the last status was received. Upon a reconnect, the client can then estimate the appropriate backlog to request. Note that the count parameter is not allowed elsewhere, including track, sample and on the default access role.
This is the reason you're getting a 413
error when you try to use the count
parameter -- you're on the "default access" role.
这篇关于如何在 tweepy 的流 API 中使用“计数"参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!