我需要开发一个应用程序,可以让我跟踪推文并将它们保存在 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数据库中?
谢谢