|
6 | 6 | - [绘制饼状图](https://matplotlib.org/gallery/pie_and_polar_charts/pie_features.html#sphx-glr-gallery-pie-and-polar-charts-pie-features-py) |
7 | 7 | - [jieba分词](https://github.com/fxsjy/jieba) |
8 | 8 | - [SnowNLP开发者文档](https://github.com/isnowfy/snownlp) |
| 9 | + |
| 10 | +步骤: |
| 11 | +1. 模拟登陆微信web版 |
| 12 | +2. 获取需要的数据 |
| 13 | +3. 对数据进行分析 |
| 14 | + |
| 15 | +所需第三方模块: |
| 16 | + |
| 17 | +* [wxpy]( https://wxpy.readthedocs.io/zh/latest/chats.html#): 微信网页版接口封装Python版本,在本文中用以获取微信好友信息 |
| 18 | +* [jieba](https://github.com/fxsjy/jieba): 结巴分词的 Python 版本,在本文中用以对文本信息进行分词处理 |
| 19 | +* [snownlp](https://github.com/isnowfy/snownlp): 一个 Python 中的中文分词模块,在本文中用以对文本信息进行情感判断。 |
| 20 | +* [matplotlib](https://matplotlib.org/): Python 中图表绘制模块,在本文中用以绘制柱形图和饼图 |
| 21 | + |
| 22 | +1. 登陆网页版微信: |
| 23 | + |
| 24 | +``` |
| 25 | +from wxpy import * |
| 26 | +# 初始化机器人,扫码登陆 |
| 27 | +# bot = Bot() |
| 28 | +bot = Bot(console_qr=True, cache_path=True) # 保留缓存自动登录 |
| 29 | +``` |
| 30 | +2. 获取数据 |
| 31 | + |
| 32 | +``` |
| 33 | +friends = bot.friends() |
| 34 | +``` |
| 35 | +返回的friends对象是一个包含当前用户的集合.所以取数据的时候采用friends[1:] |
| 36 | +好友的数据包括remark_name备注名称,sex性别,province省,city市, signature签名,headimage头像 |
| 37 | +这次我只分析了前面的name,sex,province,city,signature |
| 38 | + |
| 39 | +3. 数据分析 |
| 40 | + 3.1 总体分析 |
| 41 | + |
| 42 | + |
| 43 | +``` |
| 44 | +# 总体分析 |
| 45 | +def analyseTotal(friends): |
| 46 | + result = friends.stats_text() |
| 47 | + print(result) |
| 48 | +``` |
| 49 | +3.2 具体分析 |
| 50 | +``` |
| 51 | +def analyseConcrete(friends): |
| 52 | + text = friends.stats() |
| 53 | + print('sex:',text['sex']) |
| 54 | + print('province:',text['province']) |
| 55 | + print('city:',text['city']) |
| 56 | + for friend in friends[1:]: |
| 57 | + print(friend.name,friend.sex,friend.province,friend.city,friend.signature) |
| 58 | +``` |
| 59 | +3.3 对性别分析 |
| 60 | + |
| 61 | +``` |
| 62 | +# 性别分析,饼状图显示 |
| 63 | +def analyseSex(friends): |
| 64 | + text = friends.stats() |
| 65 | + male = text['sex'][1] |
| 66 | + female = text['sex'][2] |
| 67 | + unknown = text['sex'][0] |
| 68 | + labels = 'male','female','unknown' |
| 69 | + sizes = [male,female,unknown] |
| 70 | + explode = (0, 0.1, 0) # only "explode" the 2nd slice (i.e. 'Hogs') |
| 71 | +
|
| 72 | + fig1, ax1 = plt.subplots() |
| 73 | + ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', |
| 74 | + shadow=True, startangle=90) |
| 75 | + ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. |
| 76 | +
|
| 77 | + plt.show() |
| 78 | +``` |
| 79 | + |
| 80 | +3.4 对签名进行分析 |
| 81 | +``` |
| 82 | +# 分析个性签名 |
| 83 | +def analyseSignature(friends): |
| 84 | + signatures = '' |
| 85 | + emotions = [] |
| 86 | + pattern = re.compile("lf\d.+") |
| 87 | + for friend in friends[1:]: |
| 88 | + signature = friend.signature |
| 89 | + if signature != None: |
| 90 | + signature = signature.strip().replace('span','').replace('class','').replace('emoji','') |
| 91 | + signature = re.sub(r'lf(\d.+)','',signature) |
| 92 | + # print(signature) |
| 93 | + if len(signature) > 0: |
| 94 | + nlp = SnowNLP(signature) |
| 95 | + emotions.append(nlp.sentiments) |
| 96 | + signatures += ''.join(jieba.analyse.extract_tags(signature,5)) |
| 97 | + # with open('signatures.txt', 'wt', encoding='utf-8') as file: |
| 98 | + # file.write(signatures) |
| 99 | + # Signature Emotional Judgment |
| 100 | + count_good = len(list(filter(lambda x: x > 0.66, emotions))) |
| 101 | + count_normal = len(list(filter(lambda x: x >= 0.33 and x <= 0.66, emotions))) |
| 102 | + count_bad = len(list(filter(lambda x: x < 0.33, emotions))) |
| 103 | + labels = [u'负面消极', u'中性', u'正面积极'] |
| 104 | + values = (count_bad, count_normal, count_good) |
| 105 | + plt.rcParams['font.sans-serif'] = ['simHei'] |
| 106 | + plt.rcParams['axes.unicode_minus'] = False |
| 107 | + plt.xlabel(u'情感判断') |
| 108 | + plt.ylabel(u'频数') |
| 109 | + plt.xticks(range(3), labels) |
| 110 | + plt.legend(loc='upper right', ) |
| 111 | + plt.bar(range(3), values, color='rgb') |
| 112 | + plt.title(u'%s的微信好友签名信息情感分析' % friends[0]) |
| 113 | + plt.show() |
| 114 | +``` |
| 115 | + |
| 116 | + |
| 117 | +最后: |
| 118 | +上个完整代码: |
| 119 | + |
| 120 | +``` |
| 121 | +from wxpy import * |
| 122 | +import jieba |
| 123 | +import re |
| 124 | +from snownlp import SnowNLP |
| 125 | +import jieba.analyse |
| 126 | +import matplotlib.pyplot as plt |
| 127 | +
|
| 128 | +bot = Bot(console_qr=True, cache_path=True) # 登陆一次后利用缓存登陆 |
| 129 | +# bot =Bot() # 每次都需要登陆 |
| 130 | +friends = bot.friends() |
| 131 | +
|
| 132 | +# 总体分析 |
| 133 | +def analyseTotal(friends): |
| 134 | + result = friends.stats_text() |
| 135 | + print(result) |
| 136 | +
|
| 137 | +# 具体分析每个好友 |
| 138 | +def analyseConcrete(friends): |
| 139 | + text = friends.stats() |
| 140 | + print('sex:',text['sex']) |
| 141 | + print('province:',text['province']) |
| 142 | + print('city:',text['city']) |
| 143 | + for friend in friends[1:]: |
| 144 | + print(friend.name,friend.sex,friend.province,friend.city,friend.signature) |
| 145 | +
|
| 146 | +# 性别分析,饼状图显示 |
| 147 | +def analyseSex(friends): |
| 148 | + text = friends.stats() |
| 149 | + male = text['sex'][1] |
| 150 | + female = text['sex'][2] |
| 151 | + unknown = text['sex'][0] |
| 152 | + labels = 'male','female','unknown' |
| 153 | + sizes = [male,female,unknown] |
| 154 | + explode = (0, 0.1, 0) # only "explode" the 2nd slice (i.e. 'Hogs') |
| 155 | +
|
| 156 | + fig1, ax1 = plt.subplots() |
| 157 | + ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', |
| 158 | + shadow=True, startangle=90) |
| 159 | + ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. |
| 160 | + plt.show() |
| 161 | +
|
| 162 | +# 分析个性签名 |
| 163 | +def analyseSignature(friends): |
| 164 | + signatures = '' |
| 165 | + emotions = [] |
| 166 | + pattern = re.compile("lf\d.+") |
| 167 | + for friend in friends[1:]: |
| 168 | + signature = friend.signature |
| 169 | + if signature != None: |
| 170 | + signature = signature.strip().replace('span','').replace('class','').replace('emoji','') |
| 171 | + signature = re.sub(r'lf(\d.+)','',signature) |
| 172 | + # print(signature) |
| 173 | + if len(signature) > 0: |
| 174 | + nlp = SnowNLP(signature) |
| 175 | + emotions.append(nlp.sentiments) |
| 176 | + signatures += ''.join(jieba.analyse.extract_tags(signature,5)) |
| 177 | + # with open('signatures.txt', 'wt', encoding='utf-8') as file: |
| 178 | + # file.write(signatures) |
| 179 | + # Signature Emotional Judgment |
| 180 | + count_good = len(list(filter(lambda x: x > 0.66, emotions))) |
| 181 | + count_normal = len(list(filter(lambda x: x >= 0.33 and x <= 0.66, emotions))) |
| 182 | + count_bad = len(list(filter(lambda x: x < 0.33, emotions))) |
| 183 | + labels = [u'负面消极', u'中性', u'正面积极'] |
| 184 | + values = (count_bad, count_normal, count_good) |
| 185 | + plt.rcParams['font.sans-serif'] = ['simHei'] |
| 186 | + plt.rcParams['axes.unicode_minus'] = False |
| 187 | + plt.xlabel(u'情感判断') |
| 188 | + plt.ylabel(u'频数') |
| 189 | + plt.xticks(range(3), labels) |
| 190 | + plt.legend(loc='upper right', ) |
| 191 | + plt.bar(range(3), values, color='rgb') |
| 192 | + plt.title(u'%s的微信好友签名信息情感分析' % friends[0]) |
| 193 | + plt.show() |
| 194 | +
|
| 195 | +def main(): |
| 196 | + analyseTotal(friends=friends) |
| 197 | + # analyseConcrete(friends=friends) |
| 198 | + analyseSex(friends=friends) |
| 199 | + analyseSignature(friends=friends) |
| 200 | + x = input('输入任意字符退出') |
| 201 | +
|
| 202 | +main() |
| 203 | +
|
| 204 | +``` |
| 205 | + |
0 commit comments