使用 tweepy 获取用户关注的用户的推文 [英] Get to stream tweets from users a user is following using tweepy
问题描述
我正在使用 Tweepy Streaming 方法尝试获取来自特定用户的所有推文,包括用户创建的推文和来自用户关注的其他用户的推文.
I'm using Tweepy Streaming methods to try to get all the tweets from a specific user, both the ones the user created and those coming to his timeline from other users the user is following.
我尝试了几种组合,比如这个,使用用户 ID,只给我用户推送的推文:
I've tried several combinations, this one, for instance, using the user ID, only gives me the tweets that the user pushes:
l = StreamListener()
streamer = tweepy.Stream(auth=auth, listener=l)
streamer.filter(follow=['3071126254'])
其余的试验都让我不断收到来自 Twitter 的 406 条回复:
The rest of trials all lead me to constant 406 responses from Twitter:
l = StreamListener()
streamer = tweepy.Stream(auth=auth, listener=l)
streamer.filter(follow=[''])
或
l = StreamListener()
streamer = tweepy.Stream(auth=auth, listener=l)
streamer.filter()
知道这是否可能吗?我总是可以使用 REST API 来查询用户时间线,然后确保我没有存储重复的推文……但我无法从用户时间轴流式传输所有推文,这看起来很奇怪……
Any idea if this is possible? I could always use the REST API to query for the user timeline and then make sure I'm not storing duplicated tweets...but it looks weird that I can't stream all tweets from a users timeline...
谢谢,亚历杭德罗
推荐答案
如果有人遇到同样的情况,请仔细查看官方 Twitter 文档,它指出:
If anyone runs into the same situation, having a close look at the official Twitter docs, it states:
with 参数控制接收到的消息类型.这用户流的默认值是 with=followings,它提供有关用户和关于用户的关注(帐户经过身份验证的用户跟随).设置 with=user 只发送事件关于用户,而不是关于他们关注的事件.
The with parameter controls the types of messages received. The default for User Streams is with=followings, which gives data about the user and about the user’s followings (accounts which the authenticated user follows). The setting with=user sends only events about the user, and not events about their followings.
因此,似乎正确的选项是使用用户流而不是过滤器,然后,这是有效的:
Thus, it seems that the correct option is using userstream instead of filter, then, this works:
l = StreamListener()
streamer = tweepy.Stream(auth=auth, listener=l)
streamer.userstream(_with='followings')
然后您将收到 on_data 回调方法的通知.希望这会有所帮助!
Then you'll get notified on the on_data callback method. Hope this helps!
这篇关于使用 tweepy 获取用户关注的用户的推文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!