Python 使用与给定标记匹配的tweet维护mongodb
我必须保留一个mongodb,其中包含与给定标签匹配的推文。而且,我必须用推特的最新数据来更新它。我需要创建“id_str”索引。 运行此代码时发生异常,但不知道原因。只是我知道“for”中有个问题Python 使用与给定标记匹配的tweet维护mongodb,python,mongodb,twitter,twython,Python,Mongodb,Twitter,Twython,我必须保留一个mongodb,其中包含与给定标签匹配的推文。而且,我必须用推特的最新数据来更新它。我需要创建“id_str”索引。 运行此代码时发生异常,但不知道原因。只是我知道“for”中有个问题 def getSearchTagTwitter(tag): db = connexMongoDB() t = loginTwython2() search = t.search(q=tag, count=100) data = search['statuses'] try:
def getSearchTagTwitter(tag):
db = connexMongoDB()
t = loginTwython2()
search = t.search(q=tag, count=100)
data = search['statuses']
try:
for tweet in data:
db.twittersearch.ensureIndex(tweet['id_str'])
db.twittersearch.update(tweet)
except Exception:
print "Error tag"
time.sleep(60*15)
getSearchTagTwitter(tag)
我认为这个错误是由
ensureIndex
引起的,因为语法看起来不太正确。正确的语法是:
db.twittersearch.ensureIndex('id_str')
也不需要在for循环的每次迭代中创建索引,只需声明一次,它就会自动更新。请注意,ensureIndex
现在已不推荐使用,您应该使用create_index
此外,您的update
语句看起来也不正确,应该如下所示:
db.twittersearch.update_one({"id_str":"12344"}, tweet, upsert=True)
这似乎是解决方案,但仍然不起作用。有什么想法吗?我想避免重复。我可以用“save”来做吗?“save”已被弃用