【发布时间】:2014-01-18 18:03:06
【问题描述】:
我希望在 MongoDB 中存储 x # 条推文后,让 Tweepy Streaming API 停止提取推文。
我在类中尝试了 IF 和 WHILE 语句,用计数器定义,但无法让它在某个 X 量处停止。这对我来说是一个真正的头脑风暴。我在这里找到了这个链接:https://groups.google.com/forum/#!topic/tweepy/5IGlu2Qiug4 但我复制它的努力失败了。它总是告诉我 init 需要一个额外的参数。我相信我们的 Tweepy 身份验证设置是不同的,所以它不是苹果对苹果。
有什么想法吗?
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json, time, sys
import tweepy
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
class StdOutListener(StreamListener):
def on_status(self, status):
text = status.text
created = status.created_at
record = {'Text': text, 'Created At': created}
print record #See Tweepy documentation to learn how to access other fields
collection.insert(record)
def on_error(self, status):
print 'Error on status', status
def on_limit(self, status):
print 'Limit threshold exceeded', status
def on_timeout(self, status):
print 'Stream disconnected; continuing...'
stream = Stream(auth, StdOutListener())
stream.filter(track=['tv'])
【问题讨论】: