How can I consume tweets from Twitter's streaming api and store them in mongodb

后端 未结 2 1509
被撕碎了的回忆
被撕碎了的回忆 2020-12-23 06:16

I need to develop an app that lets me track tweets and save them in a mongodb for a research project (as you might gather, I am a noob, so please bear with me). I have foun

相关标签:
2条回答
  • 2020-12-23 06:20

    Here's an example:

    import json
    import pymongo
    import tweepy
    
    consumer_key = ""
    consumer_secret = ""
    access_key = ""
    access_secret = ""
    
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_key, access_secret)
    api = tweepy.API(auth)
    
    
    class CustomStreamListener(tweepy.StreamListener):
        def __init__(self, api):
            self.api = api
            super(tweepy.StreamListener, self).__init__()
    
            self.db = pymongo.MongoClient().test
    
        def on_data(self, tweet):
            self.db.tweets.insert(json.loads(tweet))
    
        def on_error(self, status_code):
            return True # Don't kill the stream
    
        def on_timeout(self):
            return True # Don't kill the stream
    
    
    sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))
    sapi.filter(track=['Gandolfini'])
    

    This will write tweets to the mongodb test database, tweets collection.

    Hope that helps.

    0 讨论(0)
  • 2020-12-23 06:46

    I have developed a simple command line tool that does exactly this.

    https://github.com/janezkranjc/twitter-tap

    It allows using the streaming API or the search API.

    0 讨论(0)
提交回复
热议问题