使用Tweepy仅从朋友打印推文

时间:2014-10-11 23:31:45

标签: python twitter tweepy

我在过去的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)

1 个答案:

答案 0 :(得分:1)

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

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

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

P.S。这不是一个主要问题,但您在代码中进行了两次身份验证,这是不必要的。