跟踪哪个(tweepy)过滤器发送了推文
问题描述:
我需要跟踪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搜寻)
答
我不知道怎么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)
最多涨涨涨涨涨涨 –