我需要开发一个应用程序,让我可以跟踪推文并将它们保存在 mongodb 中以用于研究项目(您可能会收集到,我是菜鸟,所以请多多包涵)。我发现这段代码通过我的终端窗口发送推文:
import sys
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):
print status.text
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True # Don't kill the stream
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(track=['Gandolfini'])
有没有办法可以修改这段代码,而不是让推文在我的屏幕上流式传输,而是将它们发送到我的 mongodb 数据库?
谢谢