吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 17676|回复: 69
收起左侧

[Python 转载] 使用python获取X信群聊信息并进行分析

  [复制链接]
ProgramerFangL 发表于 2018-9-26 16:55
本帖最后由 wushaominkk 于 2019-6-6 21:02 编辑

在之前用python的itchat玩了一波微信好友以后,最近突然想继续玩一下微信群聊。说做就做,就以公司的微信群来一波操作吧。
代码主要实现的功能主要是,获取微信群聊,获取群聊中的用户信息,以及根据这些信息制作的柱状图,饼图和词云。
代码在最后。先来看一波结果,首先是单位微信群的性别统计图(忽视我的视觉审美):
Figure_1.png
这未知是个什么鬼啊。。。好吧,因为性别代号1为男,2为女,最后发现还有0存在,所以就弄成未知了,希望这些同事知道了不要打我。。。。

然后是分别对男同事和女同事的个性签名制作了词云,女同事的画风是这样的:
Figure_3.png
看起来很注重健康,也很喜欢旅行,想要的是理想,接下来是男同志的了:
Figure_4.png
卧槽,这画风啥意思啊!!!!!怎么到了我们男同胞就变成了自己!努力!人生!还要后天下之乐而乐。。。好沉重的话题,大家的觉悟都很高,不过貌似也对哈。

下面上场的就是一个很俗但是还蛮好看的微信昵称词云图了:
InkedFigure_2_LI.jpg
虽然图其实很炫酷,但是也不能泄露同事的隐私噻,所以呢我就把图修了半天,无奈PS技能没加点,只好手动打了码。只留下这个酷酷的与你无关

最后的这个图是位置统计图:
捕获test.PNG
公司竟然有这么多的北京大佬,牛皮牛皮。

代码就贴这儿了,如果大家有什么新需求,可以一起做啊。
#!/usr/bin/python
# coding: utf-8
import itchat
import os
import pandas as pd
import matplotlib.pyplot as plot
from wordcloud import WordCloud
from pyecharts import Bar, Page
import jieba

import sys

reload(sys)
sys.setdefaultencoding('utf-8')

# 获取所有的群聊列表
def getRoomList():
    roomslist = itchat.get_chatrooms()
    return roomslist
#获取指定群聊的信息
def getRoomMsg(roomName):
    itchat.dump_login_status()
    myroom = itchat.search_chatrooms(name=roomName)
    return myroom
#统计省份信息
def getProvinceCount(cityCount,countName):
    indexs = []
    counts = []
    for index in cityCount.index:
        indexs.append(index)
        counts.append(cityCount[index])
    page = Page()
    labels = [indexs]
    sizes = [counts]
    attr = indexs
    v1 = counts
    bar = Bar(countName)
    bar.add("地区分布", attr, v1, is_stack=True, is_label_show=True, is_datazoom_show=True,
            is_random=True)
    page.add(bar)
    bar.show_config()
    bar.render()
#制作性别统计图
def getSexCount(sexs,countName):
    labels = [u'男', u'女', u'未知']
    sizes = [sexs['男'], sexs['女'], sexs['未知']]
    print sizes
    colors = ['red', 'yellow', 'blue', 'green']
    explode = (0, 0, 0)
    patches, l_text, p_text = plot.pie(sizes, explode=explode, labels=labels, colors=colors,
                                       labeldistance=1.1, autopct='%2.0f%%', shadow=False,
                                       startangle=90, pctdistance=0.6)
    for t in l_text:
        t.set_size = 30
    for t in p_text:
        t.set_size = 20
    plot.axis('equal')
    plot.legend(loc='upper left', bbox_to_anchor=(-0.1, 1))
    plot.rcParams['font.sans-serif'] = ['Microsoft YaHei']
    plot.rcParams['axes.unicode_minus'] = False
    plot.title(countName)
    plot.grid()
    plot.show()
#制作词云
def makeWorldCount(userName):
    users = []
    for user in userName:
        users.append(user)
    users = ','.join(users)
    print users
    font = os.path.join(os.path.dirname(__file__), "DroidSansFallbackFull.ttf")
    wordcloud = WordCloud(font_path=font,width=1800, height=800, min_font_size=4, max_font_size=80,margin=2).generate(users)
    plot.figure()
    plot.imshow(wordcloud, interpolation='bilinear')
    plot.axis("off")
    plot.show()
#获取性别统计
def getSex(df_friends):
    sex = df_friends['Sex'].replace({1: '男', 2: '女', 0: '未知'})
    sexCount = sex.value_counts()
    return sexCount
#男性个性签名统计
def analyMale(df_friends):
    signature = df_friends[df_friends.Sex == 1]['Signature']
    signature = signature.unique()
    signature = "".join(signature)
    wordlist_after_jieba = jieba.cut(signature, cut_all=True)
    wl_space_split = " ".join(wordlist_after_jieba)
    font = os.path.join(os.path.dirname(__file__), "DroidSansFallbackFull.ttf")
    wordcloud = WordCloud(font_path=font,max_words = 200, max_font_size=50,margin=2).generate(wl_space_split)
    plot.figure()
    plot.imshow(wordcloud, interpolation='bilinear')
    plot.axis("off")
    plot.show()
#女性个性签名统计
def analyFemale(df_friends):
    signature = df_friends[df_friends.Sex == 2]['Signature']
    signature = signature.unique()
    signature = "".join(signature)
    wordlist_after_jieba = jieba.cut(signature, cut_all=True)
    wl_space_split = " ".join(wordlist_after_jieba)
    font = os.path.join(os.path.dirname(__file__), "DroidSansFallbackFull.ttf")
    wordcloud = WordCloud(font_path=font,max_words = 200, max_font_size=50,margin=2).generate(wl_space_split)
    plot.figure()
    plot.imshow(wordcloud, interpolation='bilinear')
    plot.axis("off")
    plot.show()
def main():
    itchat.auto_login(hotReload=True)    #自动登陆
    roomMsg = getRoomMsg(u'') #获取指定群聊的信息
    gsq = itchat.update_chatroom(roomMsg[0]['UserName'], detailedMember=True)
    df_friends = pd.DataFrame(gsq['MemberList'])    #取出其中的用户信息并转为dataframe
    # sexs = getSex(df_friends)   #获取性别统计
    # getSexCount(sexs,"公司性别统计图")    #制作性别统计图,第一个参数为性别统计的结果,第二个参数为该图的名称
    # city = df_friends['Province']   #取出省份信息
    # City_count = city.value_counts()[:15]
    # City_count = City_count[City_count.index != '']
    # getProvinceCount(City_count,"位置统计图")     #制作位置统计图,第一个参数为位置统计的结果,第二个参数为该图的名称
    # makeWorldCount(df_friends['NickName'])  #制作词云,传入用户昵称
    # makeWorldCount(signature)
    analyMale(df_friends)
if __name__=='__main__':
   main()

免费评分

参与人数 18吾爱币 +17 热心值 +16 收起 理由
小痞仔 + 1 我很赞同!
abcdeboy + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
helian147 + 1 我很赞同!
nanmobei + 1 + 1 我很赞同!
tingge3322 + 1 + 1 热心回复!
sadsadan + 1 + 1 谢谢@Thanks!
+傻托 + 1 谢谢@Thanks!
小蜗 + 1 + 1 热心回复!
问苍天 + 1 + 1 谢谢@Thanks!
wushaominkk + 3 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
l12h1 + 1 + 1 用心讨论,共获提升!
demontan + 1 + 1 用心讨论,共获提升!
Learning_deng + 1 + 1 用心讨论,共获提升!
无辰 + 1 + 1 送分!!!!!!!!
John_Mac + 1 热心回复!
莫名堂 + 1 谢谢@Thanks!
一个小将 + 1 + 1 谢谢@Thanks!
红糖小园子 + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| ProgramerFangL 发表于 2018-9-26 17:24
免费的评分给我送点儿啊各位,码字也很累的好吗。。。
 楼主| ProgramerFangL 发表于 2018-9-26 17:12
beijinwork 发表于 2018-11-13 04:38
Traceback (most recent call last):
  File "c:\python\python零基础视频教程\爬虫day06\爬虫\taobao.py", line 5, in <module>
    import pandas as pd
  File "C:\Python\Python37-32\lib\site-packages\pandas\__init__.py", line 35, in <module>
    "the C extensions first.".format(module))
ImportError: C extension: 'lib' from 'pandas._libs' (C:\Python\Python37-32\lib\site-packages\pandas\_libs\__init__.py) not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.
>>>
红糖小园子 发表于 2018-9-26 17:01
楼主可以 啊 源码已下 研究一哈
kk1212 发表于 2018-9-26 17:21
大牛啊 ,你这个厉害
于香肉斯 发表于 2018-9-26 17:26
资瓷资瓷
一个小将 发表于 2018-9-26 17:48
虽然不知道咋玩,但是给你顶个
niebaohua 发表于 2018-9-26 17:52
支持一下  不过我好像没有评分了= =
freeflyer 发表于 2018-9-26 18:10
Python果然是好玩又厉害啊!!!
 楼主| ProgramerFangL 发表于 2018-9-26 18:13
freeflyer 发表于 2018-9-26 18:10
Python果然是好玩又厉害啊!!!

你的头像也很厉害
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-27 07:57

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表