python操作Mongo数据库

本文通过Python脚本从MongoDB数据库中抓取张艺兴微博的相关数据,并计算了包括粉丝数、评论、转发及点赞等指标的总量和平均值。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import operator
from pymongo import MongoClient
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

DBC_W_RANK = MongoClient("192.168.1.10").game.weiboRank
DBC_W_ANALYSE = MongoClient("192.168.1.10").game.weiboAnalyse

def getZhangyixing():

print "名称,id,粉丝数,评论总量,转发总量,发博总量,点赞总量,平均评论量,平均转发量,平均发博量,平均点赞量\n"
for item in DBC_W_ANALYSE.find({"weiboId":"2706896955"}):
weiboId = str(item.get("weiboId"))
postStats = item.get("postStat")
leng = len(postStats)
totalC = 0
totalF = 0
totalP = 0
totalL = 0
for item in postStats:
totalC=item["commentCount"] + totalC
totalF = item["forwardCount"] + totalF
totalP = item["postCount"] + totalP
totalL = item["likeCount"] + totalL
avgC = totalC / float(leng)
avgF = totalF / float(leng)
avgP = totalP / float(leng)
avgL = totalL / float(leng)
string = "努力努力再努力x" + ","+str(weiboId)+","+" dd "+"," + str(totalC) + "," + str(totalF) + "," + str(totalP) + ","+ str(totalL)+ ","+ str(avgC) + "," + str(avgF) + "," + str(avgP)+ "," + str(avgL) + "\n"
print string
if __name__ == "__main__":
getZhangyixing()

转载于:https://www.cnblogs.com/sy-liu/p/7058283.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值