Linking

Capturing Life & Tech

  • 主页
  • 随笔
  • 关于我
所有文章 外链

Linking

Capturing Life & Tech

  • 主页
  • 随笔
  • 关于我

微信好友分析

阅读数:次 2017-08-09
字数统计: 950字   |   阅读时长≈ 4分

目录

  • 1.微信好友性别比例分析
  • 2.好友个性签名词云

一个好玩的例子,分析自己的微信好友。做了两个,一个是好友性别比例分析,另一个是好友的个性签名做一个个性化的词云。

这两个实验在好友量较多时具有一定的价值,我的好友姑且做一个测试吧。

1.微信好友性别比例分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import itchat

# 登录微信,需要扫描弹出的二维码
itchat.login()
# 爬取好友信息,json字符串
friends = itchat.get_friends(update=True)[0:]
# print(friends) # 测试是否正常输出好友

# 定义一个函数,用来爬取各个变量, return list arr
def get_var(var):
variable = []
for i in friends:
value = i[var]
variable.append(value)
return variable

Sex = get_var('Sex')
# 初始化男女性别计数器
male = female = other = 0
for sex in Sex:
if sex == 1:
male += 1
elif sex == 2:
female += 1
else:
other += 1

# 总数
total = len(friends)

maleRate = float(male)/total*100
femaleRate = float(female)/total*100
otherRate = float(other)/total*100

print('男性好友%d人,占比:%.2f%%' % (male, maleRate))
print('女性好友%d人,占比:%.2f%%' % (female, femaleRate))
print('其他性别好友%d人,占比:%.2f%%' % (other, otherRate))

# output
# 男性好友199人,占比:61.04%
# 女性好友112人,占比:34.36%
# 其他性别好友15人,占比:4.60%

我的好友这么少啊,男性还是占多数的。部分好友未设置性别属性。

绘制比例图

可视化展示用到了matplotlib.pyplot,看到有人用百度的echarts-python,这个库还不成熟,我用的时候遇到中文编码错误,所以弃了。改用更官方更强大的matplotlib。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 百分比圆饼表
import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'male', 'female', 'other'
sizes = [maleRate, femaleRate, otherRate]
explode = (0, 0.1, 0) # 突出显示女性比例,嘿嘿 only "explode" the 2nd slice (i.e. 'female')

fig1, ax1 = plt.subplots()

ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.2f%%',
shadow=True, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.

plt.show()

2.好友个性签名词云

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# coding: utf-8
import itchat

# 登录微信,需要扫描弹出的二维码
itchat.login()
# 爬取好友信息,json字符串
friends = itchat.get_friends(update=True)[0:]
# print(friends)

#定义一个函数,用来爬取各个变量
def get_var(var):
variable = []
for i in friends:
value = i[var]
variable.append(value)
return variable

# 调用函数得到各变量
# 个性签名
Signature = get_var('Signature')

# 很多本来是表情的,变成了 emoji、span、class 等等这些无关紧要的词,需要先替换掉,另外,
# 还有类似<>/= 之类的符号,也需要写个简单的正则替换掉,再把所有拼起来,得到 text 字串。
import re
siglist = []
for i in friends:
signature = i["Signature"].strip().replace("span", "").replace("class", "").replace("emoji", "")
rep = re.compile("1f\d+\w*|[<>/=]")
signature = rep.sub("", signature)
siglist.append(signature)
text = "".join(siglist)

# 结巴分词
import jieba
wordlist = jieba.cut(text, cut_all=True)
word_space_split = " ".join(wordlist)

# 根据自己想要的图片、形状、颜色画出相似的图形
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
import numpy as np
import PIL.Image as Image
import os

# d = os.path.dirname(__file__)
# 可选:coloring = np.array(Image.open(os.path.join(d, '/imgs/wechat.jpg)))
coloring = np.array(Image.open("{0}/imgs/wechat.jpg".format(os.getcwd()))) # 这里的wechat.jpg可以换成其他你想呈现的效果底图
my_wordcloud = WordCloud(background_color="white", max_words=2000,
mask=coloring, max_font_size=60, random_state=42, scale=2,
font_path="/Library/Fonts/songti.ttc").generate(word_space_split)

image_colors = ImageColorGenerator(coloring)
plt.imshow(my_wordcloud.recolor(color_func=image_colors))
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()

# 保存图片,并发送到手机
my_wordcloud.to_file(os.path.join(os.getcwd(), 'output/wechat_signature_cloud.jpg'))
itchat.send_image('output/wechat_signature_cloud.jpg', 'filehelper')

底图1:

词云效果图1:

底图2:

词云效果图2:

本文同时发布于个人微信公众号微信好友初步分析

  • 本文作者: Linking
  • 本文链接: https://linking.fun/2017/08/09/微信好友分析/
  • 版权声明: 版权所有,转载请注明出处!

扫一扫,分享到微信

10dayWeatherSearchfromYahoo
精通Python网络爬虫-第五章-正则表达式与Cookie的使用
  1. 1. 目录
  2. 2. 1.微信好友性别比例分析
    1. 2.1. 绘制比例图
  3. 3. 2.好友个性签名词云
© 2015-2026 Linking
GitHub:hexo-theme-yilia-plus by Litten
本站总访问量次 | 本站访客数人
  • 所有文章
  • 外链

tag:

  • weather
  • 需求
  • essay
  • basketball
  • olympic
  • nginx
  • APPScan
  • SQl盲注
  • xss
  • Ajax
  • ajax
  • ai
  • agent
  • openclaw
  • ccf
  • Nginx
  • HTML5
  • html5
  • hmtl5
  • sse
  • JavaScriptCore
  • Oracle
  • operation
  • Linux
  • deploy
  • Mac Office
  • markdown
  • ListView
  • GridView
  • MySQL
  • 慢查询
  • mongodb
  • 转置
  • thought
  • network
  • ubuntu
  • NetworkManager
  • RFKill
  • Netplan
  • avatar
  • cocoa
  • blog
  • Gitalk
  • container
  • macvlan
  • docker
  • oracle
  • cookie
  • patch
  • gitea
  • git
  • iOS
  • https
  • 多线程
  • bundle
  • 兼容性
  • HTTP
  • 绘图
  • cs
  • java
  • 效率
  • 快捷键
  • route
  • nodejs
  • pip
  • arcgis
  • arcgis 建模
  • 标识
  • redis
  • read
  • bookList
  • running
  • showdoc
  • disk
  • unit-test
  • D.Wade
  • thoughts
  • duoduo
  • Python
  • python
  • tomcat
  • 读书节
  • session
  • jdk
  • war
  • 加班
  • Android onclick事件监听
  • 正则
  • 手机品牌匹配
  • ntp
  • OpenLayers
  • Geoserver
  • wechat
  • 微信公众号
  • 爬虫
  • WeChat
  • 张靓颖
  • 动漫
  • vpn
  • PPT
  • MarkDown
  • plan
  • 朱赟
  • 极客时间专栏
  • 极客邦
  • 模块化
  • MVC
  • excel
  • NBA
  • kobe
  • team
  • crawler
  • 进度条
  • ssl
  • book
  • anti-stealing-link
  • Agentic Engineering
  • Vibe Coding
  • Software 3.0
  • Andrej Karpathy
  • LLM
  • Programming

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia-plus根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • GitHub Trending
  • OpenAI ChatGPT
  • Gitee码云
  • 简书
  • CSDN