names = ["zyl","cxk","yyqx"]
df = pd.DataFrame({})
for name in names:
res = list(db[name].aggregate([{"$match": {"publish_time": {"$gt": "2019-07-15","$lt":"2019-07-24"}}},
{"$sort": {"sum": -1}},
{"$group": {"_id": {"uid": "$uid", "publish_time": "$publish_time"},
"uid": {"$first": "$uid"},
"publish_time": {"$first": "$publish_time"},
"user": {"$first": "$user"},
"mid": {"$first": "$mid"},
"forward": {"$first": "$forward"},
"comment": {"$first": "$comment"},
"like": {"$first": "$like"},
"count": {"$sum": 1},
}
}]))
df1 = pd.DataFrame(res)
df2 =pd.DataFrame(list(db.fensinfo.find({"star_name":{"$in":["朱一龙","蔡徐坤","易烊千玺","李现"]}})))
df3 = pd.merge(df1,df2,left_on="uid",right_on="id") # 横向连接
df = pd.concat([df,df3],axis=0, ignore_index=True) # 纵向连表
df.sort_values("publish_time",inplace=True,kind='mergesort')
df.sort_values("star_name",inplace=True,kind='mergesort')
df.to_excel("转赞评数.xlsx",index=False,columns=["uid","screen_name","star_name","followers_count","forward","comment","like","count","type1","type2","publish_time"]
,header=["uid","昵称","明星","粉丝数","转发数","评论数","点赞数","当日微博数","大分类","小分类","发布时间"])
import pandas as pd
df1 = pd.DataFrame(list(db.interaction.find()))
df2 =pd.DataFrame(list(db.fensinfo.find({"star_name":{"$in":["朱一龙","蔡徐坤","易烊千玺","李现"]}})))
df3 = pd.merge(df1,df2,left_on="uid",right_on="id")
df4 = df3[["uid","screen_name","star_name","followers_count","weibos","interaction","reads","date"]]
df4.sort_values("date",inplace=True)
df4.sort_values("star_name",inplace=True,kind='mergesort')
df4.to_excel("互动数.xlsx",index=False,columns=["uid","screen_name", "star_name","followers_count", "weibos","interaction","reads","date"],
header=["uid","昵称", "明星","粉丝数", "昨日微博数", "互动数", "阅读数","日期"], )