【问题标题】:Using Tweepy to print tweets just from your friends使用 Tweepy 打印来自您朋友的推文
【发布时间】:2014-12-06 20:35:16
【问题描述】:

在过去的 3 个小时里,我一直在试图找出 Tweepy,但我仍然卡住了。 我希望能够获取我朋友在 2014 年 9 月至 10 月期间的所有推文,并通过转发的前 10 名对其进行过滤。

我对 StreamListener 只是模糊地熟悉,但是,我认为它会列出实时的推文。我想知道我是否可以回到上个月并从我的朋友那里获取那些推文。这可以通过 Tweepy 完成吗?这是我现在拥有的代码。

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener

import csv

ckey = 'xyz'
csecret = 'xyz'
atoken = 'xyz'
asecret = 'xyz'

auth = OAuthHandler(ckey,csecret)
auth.set_access_token(atoken, a secret)

class Listener(StreamListener):
    def on_data(self,data):
        print data
        return True
    def on_error(self,status):
        print status

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
tiwtterStream = Stream(aut, Listener())

users = [123456, 7890019, 9919038] # this is the list of friends I would like to output
twitterStream.filter(users, since=09-01-2014, until = 10-01-2014)

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    StreamListener 返回实时推文是正确的。要从特定用户获取过去的推文,您需要使用 tweepy 的 API 包装器--tweepy.API。一个示例,它将从您的 Listener 类中替换下来:

    api = tweepy.API(auth)
    tweetlist = api.user_timeline(id=123456)
    

    这将返回最多 20 个 status 对象的列表。您可以使用parameters 来获得更多结果,可能count 和since 将有助于您的实施。我认为您对单个count 的要求最多是200 tweets。

    附:不是主要问题,但您在代码中进行了两次身份验证,这是不必要的。

    【讨论】:

      猜你喜欢
      • 2019-06-21
      • 1970-01-01
      • 2016-11-22
      • 1970-01-01
      • 2014-11-14
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      相关资源
      最近更新 更多