2017-03-08 63 views
2

我需要跟蹤twitter上的許多關鍵字並將推文發送到MongoDB。我用這對我的代碼:跟蹤哪個(tweepy)過濾器發送了推文

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

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)) 

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

sapi.filter(track = to_track) 

有我的方式來跟蹤哪些關鍵字負責每個鳴叫來了呢? (如果沒有在做每一個grep搜尋)

+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)