试图将 twitter 流数据存储到 MongoDB 中。该代码几乎是来自http://stats.seandolinar.com/collecting-twitter-data-storing-tweets-in-mongodb/的副本, 但始终显示错误。如果我尝试打印数据,它会显示 json 文件不断增长,但尽管 while 循环有时间限制,它似乎永远不会结束。!
class listener(StreamListener):
def __init__(self, start_time, time_limit=60):
self.time = start_time
self.limit = time_limit
def on_data(self, data):
while (time.time() - self.time) < self.limit:
try:
tweet = json.loads(data)
client = MongoClient('localhost', 27017)
db = client['twitter_db']
collection = db['twitter_collection']
collection.insert_many(tweet)
return True
except BaseException, e:
print 'failed ondata,', str(e)
time.sleep(5)
pass
exit()
def on_error(self, status):
print statuses
Run Code Online (Sandbox Code Playgroud)