参考https://blog.csdn.net/qinyuanpei/article/details/79360703,基于python3.6实现微信朋友圈性别、地区、个性签名、头像四个维度的分析。

GitHub项目地址:https://github.com/KaguraTyan/wechat_analysis_itchat

1、准备工作

1.1 环境要求

  • WIN10
  • python3.6
  • pycharm编译器

1.2 第三方库要求

  • itchat:itchat是一个开源的微信个人号接口
  • jieba:结巴分词的 Python 版本,对文本信息进行分词处理。
  • matplotlib: Python 中图表绘制模块,在本文中用以绘制柱形图和饼图
  • snownlp:一个 Python 中的中文分词模块,在本文中用以对文本信息进行情感判断。
  • PIL: Python 中的图像处理模块,在本文中用以对图片进行处理。
  • numpy: Python中 的数值计算模块,在本文中配合 wordcloud 模块使用。
  • wordcloud: Python 中的词云模块,在本文中用以绘制词云图片。
  • TencentYoutuyun:腾讯优图提供的 Python 版本 SDK ,在本文中用以识别人脸及提取图片标签信息。

安装问题①:

上述几个第三方库均可通过pip install方式安装,除了TencentYoutuyun

通过普通的pip方法无法实现安装,会报错

pip install TencentYoutuyun

解决方法①:

先去github下载官方sdk的zip压缩包至某目录下,如E:\Project\test\Python_sdk-master.zip

https://github.com/Tencent-YouTu/Python_sdk

打开命令行,定位到下载目录下,再进行pip安装,至安装成功

e:
cd Project\test
pip install Python_sdk-master.zip

安装问题②:

PIL目前只支持python2.x版本,不支持python3.x。

解决方法②:

Pillow是PIL的一个派生分支,但如今已经发展成为比PIL本身更具活力的图像处理库。

Pillow的Github主页:https://github.com/python-pillow/Pillow
Pillow的文档(对应版本v3.0.0):https://pillow.readthedocs.org/en/latest/handbook/index.html
Pillow的文档中文翻译(对应版本v2.4.0):http://pillow-cn.readthedocs.org/en/latest/

Python 3.x 安装Pillow

给Python安装Pillow非常简单,使用pip或easy_install只要一行代码即可。

在命令行使用PIP安装:

pip install Pillow

或在命令行使用easy_install安装:

easy_install Pillow

安装完成后,使用from PIL import Image就引用使用库了。比如:

from PIL import Image
im = Image.open("bride.jpg")
im.rotate(45).show()

2、好友性别

获取好友性别信息,统计男、女、未知的数量,计算比例并制作饼图可视化。

def analyseSex(firends):# 将friends中的Sex信息抽取出来,map返回的是迭代器,转化为list格式sexs = list(map(lambda x:x['Sex'],friends[1:]))# item返回(键,值)元组,第一维为键,第二维为值,即个数# 取性别个数变为列表counts = Counter(sexs).items()counts = sorted(counts, key=lambda x:x[0], reverse=False)counts = list(map(lambda x:x[1],counts))labels = ['Unknow','Male','Female']colors = ['red','yellowgreen','lightskyblue']plt.figure(figsize=(8,5), dpi=80)plt.axes(aspect=1) plt.pie(counts, labels=labels, colors=colors, labeldistance = 1.1, autopct = '%3.1f%%',shadow = False, startangle = 90, pctdistance = 0.6 )plt.legend(loc='upper right',)plt.title(u'%s的微信好友性别组成' % friends[0]['NickName'])plt.savefig("analyseSex.jpg")plt.show()

3、好友地区

获取好友的地区信息,保存至本地'location.csv',再统计各地区的好友数量存至'location_analysis.xls’。

def analyseLocation(friends):freqs = {}headers = ['NickName','Province','City']with open('location.csv','w',encoding='utf-8',newline='',) as csvFile:# DictWriter以字典的形式写入内容# 设置写入格式writer = csv.DictWriter(csvFile, headers)#  writeheader()实现添加文件头(数据名)writer.writeheader()for friend in friends[1:]:row = {}row['NickName'] = friend['NickName']row['Province'] = friend['Province']row['City'] = friend['City']# 统计城市数目if(friend['Province']!=None):if(friend['Province'] not in freqs):freqs[friend['Province']] = 1else:freqs[friend['Province']] += 1writer.writerow(row)print(freqs)print(type(freqs))print(len(freqs))key_list = list(freqs.keys())value_list = list(freqs.values())book = xlwt.Workbook(encoding='utf-8')sheet = book.add_sheet('sheet1')sheet.write(0, 0, 'Province')  # 其中的'0-行, 0-列'指定表中的单元sheet.write(0, 1, 'Num')for i in range(len(freqs)):sheet.write(i+1, 0, key_list[i])sheet.write(i+1, 1, value_list[i])book.save('location_analysis.xls')

使用BDP报表工具https://me.bdp.cn/home.html分析地区信息

点击上方“数据源” ,添加数据“excel”,上传'location_analysis.xls'——新建图表——普通图表、仪表盘示例、微信运营分析——维度:province,数值:num——右侧,图表类型:地图,最后生成下图。

4、好友头像

调用腾讯优图的人脸识别接口DetectFace和图片标签识别分类接口imagetag,再生成使用人脸头像的饼图和头像标签的词云。

- 接口
`DetectFace(self, image_path, mode = 0, data_type = 0)`
- 参数- `image_path` 待检测的图片路径- `mode` 是否大脸模式,默认非大脸模式- `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url- 接口
`imagetag(self, image_path, data_type = 0, seq = '')`
- 参数- `image_path` 标识图片信息- `data_type` 用于表示image_path是图片还是url, 0代表图片,1代表url
def analyseHeadImage(frineds):# Init PathbasePath = os.path.abspath('.')baseFolder = basePath + '\\HeadImages\\'if not os.path.exists(baseFolder) :# if(os.path.exists(baseFolder) == False):os.makedirs(baseFolder)# Analyse ImagesfaceApi = FaceAPI()use_face = 0not_use_face = 0image_tags = ''for index in range(1,len(friends)):friend = friends[index]# Save HeadImagesimgFile = baseFolder + '\\Image%s.jpg' % str(index)imgData = itchat.get_head_img(userName = friend['UserName'])if not os.path.exists(imgFile):# if(os.path.exists(imgFile) == False):with open(imgFile,'wb') as file:file.write(imgData)# Detect Facestime.sleep(1)result = faceApi.detectFace(imgFile)if result == True:use_face += 1else:not_use_face += 1 # Extract Tagsresult = faceApi.extractTags(imgFile)image_tags += ','.join(list(map(lambda x:x['tag_name'],result)))labels = [u'使用人脸头像',u'不使用人脸头像']counts = [use_face,not_use_face]colors = ['red','yellowgreen','lightskyblue']plt.figure(figsize=(8,5), dpi=80)plt.axes(aspect=1) plt.pie(counts, #性别统计结果labels=labels, #性别展示标签colors=colors, #饼图区域配色labeldistance = 1.1, #标签距离圆点距离autopct = '%3.1f%%', #饼图区域文本格式shadow = False, #饼图是否显示阴影startangle = 90, #饼图起始角度pctdistance = 0.6 #饼图区域文本距离圆点距离)plt.legend(loc='upper right',)plt.title(u'%s的微信好友使用人脸头像情况' % friends[0]['NickName'])plt.savefig("analyseHeadImage.jpg")plt.show() image_tags = image_tags.encode('iso8859-1').decode('utf-8')back_coloring = np.array(Image.open('face.jpg'))wordcloud = WordCloud(font_path='simfang.ttf',background_color="white",max_words=1200,mask=back_coloring, max_font_size=85,random_state=75,width=800, height=480, margin=15)wordcloud.generate(image_tags)plt.imshow(wordcloud)plt.axis("off")plt.savefig("wordcloudHeadImage.jpg")plt.show()

 

5、好友个性签名

先获取好友签名信息,再对文本做预处理,调用SnowNLP接口对文本做情感分析。

def analyseSignature(friends):signatures = ''emotions = []pattern = re.compile("1f\d.+")for friend in friends:signature = friend['Signature']if(signature != None):signature = signature.strip().replace('span', '').replace('class', '').replace('emoji', '')signature = re.sub(r'1f(\d.+)','',signature)if(len(signature)>0):nlp = SnowNLP(signature)emotions.append(nlp.sentiments)# 关键词提取,返回5个TF/IDF权重最大的关键词signatures += ' '.join(jieba.analyse.extract_tags(signature,5))with open('signatures.txt','wt',encoding='utf-8') as file:file.write(signatures)# Sinature WordCloudback_coloring = np.array(Image.open('flower.jpg'))wordcloud = WordCloud(font_path='simfang.ttf',background_color="white",max_words=1200,mask=back_coloring, max_font_size=75,random_state=45,width=960, height=720, margin=15)wordcloud.generate(signatures)plt.imshow(wordcloud)plt.axis("off")plt.show()wordcloud.to_file('signatures.jpg')# Signature Emotional Judgmentcount_good = len(list(filter(lambda x:x>0.66,emotions)))    # 正面情感统计count_normal = len(list(filter(lambda x:x>=0.33 and x<=0.66,emotions))) # 中性情感统计count_bad = len(list(filter(lambda x:x<0.33,emotions)))     # 负面情感统计# 计算情感比例值print(count_good * 100/len(emotions))print(count_normal * 100/len(emotions))print(count_bad * 100/len(emotions))labels = [u'负面消极',u'中性',u'正面积极']values = (count_bad,count_normal,count_good)plt.rcParams['font.sans-serif'] = ['simHei'] plt.rcParams['axes.unicode_minus'] = Falseplt.xlabel(u'情感判断')plt.ylabel(u'频数')plt.xticks(range(3),labels)plt.legend(loc='upper right',)plt.bar(range(3), values, color = 'rgb')plt.title(u'%s的微信好友签名信息情感分析' % friends[0]['NickName'])plt.savefig("analyseSignatureEmotional.jpg")plt.show()

    

python数据挖掘分析微信朋友圈(调用itchat库)相关推荐

  1. Python数据 分析微信朋友圈

    个人兴趣爱好,通过python对微信朋友圈进行了分析,主要对微信好友进行提取,对好友地区分布,签名等进行可视化 需要安装包如下: pip3 install itchat pip3 install pa ...

  2. 利用python深度分析微信朋友圈好友

    最近看了wxpy这个包,感觉还不错,分析一下微信的好友. 分析的目的: 1.看看好友的性别占比.地域分布 2.分析好友的个性签名 3.对好友的签名进行情感分析 环境:python 3.6 需要的包wx ...

  3. python抓取微信朋友圈动态_2018最全如何利用Python网络爬虫抓取微信朋友圈的动态...

    今天小编给大家分享一下如何利用Python网络爬虫抓取微信朋友圈的动态信息,实际上如果单独的去爬取朋友圈的话,难度会非常大,因为微信没有提供向网易云音乐这样的API接口,所以很容易找不到门.不过不要慌 ...

  4. Python爬取微信朋友圈 ! 让屏蔽你的女神无处可躲 ?

    接下来,我们将实现微信朋友圈的爬取. 如果直接用 Charles 或 mitmproxy 来监听微信朋友圈的接口数据,这是无法实现爬取的,因为数据都是被加密的.而 Appium 不同,Appium 作 ...

  5. python 爬取微信朋友圈的一些信息

    一.工具:使用python 3.6 自带的编辑器IDEL,在命令行可以正常运行. 二.此程序用到的一些库和包: (1)itchat:itchat是一个开源的微信个人号接口,可以登录微信账号:点击打开链 ...

  6. python微信朋友圈刷图_10分钟用Python做个微信朋友圈抽奖九宫格

    最近在朋友圈看到个好玩的抽奖九宫格: 随便点开一个: 哈哈,笑出猪叫!这个玩法利用了朋友圈发长图会显示中间局部的设定,搞笑之余也为朋友圈广告营销贡献了新思路. 制作图的过程也不复杂,长图对应部分设计好 ...

  7. 分析微信朋友圈王者荣耀battle记录

    有朋友说看不懂,我做成表格. 统计每个时段battle次数.峰值在中午12点和晚上10点之后,凌晨也大有人在,我是正常人.顺手多分析了几张.你会知道谁有朋友,谁哪天休,谁平生不修善果,只爱杀人放火.谁 ...

  8. Python爬虫爬取微信朋友圈

    更多编程教程请到:菜鸟教程 https://www.piaodoo.com/ 友情链接: 高州阳光论坛https://www.hnthzk.com/ 人人影视http://www.op-kg.com/ ...

  9. Android免费的导出微信朋友圈文字和图片的工具

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴!      ...

最新文章

  1. yum 安装redis默认目录_Centos7 安装redis 详细步骤
  2. 苹果证实收购Drive.ai自动驾驶汽车初创公司
  3. [Google Guava] 2.1-不可变集合
  4. hdu1846巴什博弈(java)
  5. pku 2195 Going Home 最小费最大流问题
  6. Java的Locale类
  7. linux系统进程控制实验报告,Linux进程控制实验报告.doc
  8. node.js——阿里企业级服务框架Egg搭建
  9. SeaJS 与 RequireJS 的差异对比
  10. MySQL设置mysqld_MySQL指定mysqld启动时所加载的配置文件
  11. yii2 学习历程——添加验证码
  12. c语言程序设计第三版朱立华主编课后答案,C语言程序设计习题解析与实验指导...
  13. 数据挖掘基础知识点总结
  14. 阿里又起社交心,天天动听做音乐轻社交?
  15. 电力系统远动及其规约
  16. 离谱!全公司电脑Alt键被抠掉,就为防员工摸鱼...
  17. Ubuntu下连接红米2无法找到设备解决方案
  18. 【踩坑】mirai登陆失败反复验证码或提示登录存在安全风险或提示版本过低的解决方法
  19. Nature子刊:教你零基础开展微生物组数据分析和可视化
  20. iOS与安卓的区别 浅谈ios为什么比安卓流畅

热门文章

  1. 推荐个网址导航,非常好用
  2. Python可以用来做什么【python应用场景】
  3. 战神Z7无线网络WLAN无法打开(开关打开后又自动关闭)
  4. 电脑重装系统后找不到硬盘怎么办
  5. 系统学习深度学习(十二)--池化
  6. 基于HTML5的APP开发学习(一)
  7. errmsg:invalid message type hint 群发图文消息
  8. css删格系统填充颜色,CSS 栅格系统
  9. S12. 中介者模式
  10. Spark 机器学习库【MLlib】编程指南