使用python下载多条tweet

2024-04-19 18:48:31 发布

您现在位置:Python中文网/ 问答频道 /正文

import tweepy 

consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)


results = api.search(q="Indian Election")

for result in results:
    print result.text

我只能下载几条tweets我怎么能下载1000条tweets或者不停地下载tweet呢


Tags: keyimporttokenauthapisecretaccessconsumer
2条回答

看看这个:
http://fidlr.org/post/51968189300/fetching-tweets-using-tweepy

您需要创建一个侦听器:

text = []
class LimitedListener(StreamListener):
    def on_data(self, data):
        if data != '':
            text.append(data)
        return True

{Twitter>使用Twitter}搜索结果:

发件人:https://github.com/tweepy/tweepy/blob/master/examples/streaming.py

class StdOutListener(StreamListener):
    """ A listener handles tweets are the received from the stream.
    This is a basic listener that just prints received tweets to stdout.

    """
    def on_data(self, data):
        print data
        return True

    def on_error(self, status):
        print status

if __name__ == '__main__':
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    stream = Stream(auth, l)
    stream.filter(track=['basketball'])

相关问题 更多 >