跟踪哪个(tweepy)过滤器发了一条推文

时间:2017-03-08 09:01:32

标签: python twitter tweepy

我需要在Twitter上跟踪许多关键字并将推文发送到MongoDB。我用它来代码:

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

class Page_1_Controller extends BaseController {
  ajaxMethod(someProperty?) {
    if (someProperty) {
      ...
    }
    else {
      super.ajaxMethod()
    }
  }
}

有没有办法让我跟踪哪个关键字对每条推文负责? (不在每个人中进行grep搜索)

1 个答案:

答案 0 :(得分:1)

我不确定on_data函数是如何工作的,但您可以使用on_status并执行以下操作:

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 on_status(self, status):
        tweet = status.text        
        words = tweet.split()
        if 'keyword1' in words:
            print "do something with keyword1"
            self.db.tweets.insert(json.loads(tweet))
        if 'keyword2' in words:
            print "do something with keyword2"
            self.db.tweets.insert(json.loads(tweet))
        if 'keyword3' in words:
            print "do something with keyword3"
            self.db.tweets.insert(json.loads(tweet))
sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))

to_track = ['keyword1', 'keyword2', 'keyword3']

sapi.filter(track = to_track)