我正在尝试在Python和MongoDB之间建立连接来存储推文。我的问题是我在Mylistener
的每个请求中得到401。你能帮忙解决这个问题吗?
class MyListener(StreamListener):
def __init__(self, start_time, time_limit=60):
self.time = start_time
self.limit = time_limit
self.tweet_data = []
def on_data(self, data):
while (time.time() - self.time)<self.limit:
try:
client = MongoClient('localhost',27017)
client.server_info()
db = client.tweets_db
collection = db.collect_tweets
tweet = json.loads(data)
collect_tweets.insert(tweet)
return True
except BaseException as e:
print("Error on_data: %s" % str(e))
return True
def on_error(self, status):
print(status)
Keywords_list=['word1','word2','word3']
twitter_stream = Stream(auth, MyListener(start_time, time_limit))
答案 0 :(得分:0)
在创建新的MongoClient实例后,您需要立即对数据库进行身份验证:
client.tweets_db.authenticate('user', 'password')
有关更详细的示例,请参阅here