|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +@author: 闲欢 |
| 5 | +""" |
| 6 | +import itchat |
| 7 | +from matplotlib import pyplot as plt |
| 8 | +import numpy as np |
| 9 | +import jieba.analyse |
| 10 | +from PIL import Image |
| 11 | +import os |
| 12 | +from matplotlib.font_manager import FontProperties |
| 13 | +from wordcloud import WordCloud |
| 14 | + |
| 15 | + |
| 16 | +class WxFriends: |
| 17 | + province_dict = {} |
| 18 | + friends = [] |
| 19 | + province_tuple = ['北京', '上海', '天津', '重庆', '黑龙江', '吉林', '辽宁', '内蒙古', '河北', |
| 20 | + '山东', '河南', '安徽','江苏', '浙江', '福建', '广东', '湖南', '湖北', '江西', |
| 21 | + '宁夏', '甘肃', '新疆', '西藏', '青海', '四川','云南', '贵州', '广西', '山西', |
| 22 | + '陕西', '海南', '台湾'] |
| 23 | + |
| 24 | + # 登录 |
| 25 | + @staticmethod |
| 26 | + def login(): |
| 27 | + itchat.auto_login() |
| 28 | + |
| 29 | + # 爬取好友数据 |
| 30 | + def crawl(self): |
| 31 | + friends_info_list = itchat.get_friends(update=True) |
| 32 | + print(friends_info_list[1]) |
| 33 | + for friend in friends_info_list: |
| 34 | + |
| 35 | + print(friend['NickName'], friend['RemarkName'], friend['Sex'], friend['Province'], friend['Signature']) |
| 36 | + |
| 37 | + # 处理省份 |
| 38 | + if friend['Province'] in self.province_dict: |
| 39 | + self.province_dict[friend['Province']] = self.province_dict[friend['Province']] + 1 |
| 40 | + else: |
| 41 | + if friend['Province'] not in self.province_tuple: |
| 42 | + if '海外' in self.province_dict.keys(): |
| 43 | + self.province_dict['海外'] = self.province_dict['海外'] + 1 |
| 44 | + else: |
| 45 | + self.province_dict['海外'] = 1 |
| 46 | + else: |
| 47 | + self.province_dict[friend['Province']] = 1 |
| 48 | + |
| 49 | + self.friends.append(friend) |
| 50 | + |
| 51 | + # 保存头像 |
| 52 | + img = itchat.get_head_img(userName=friend["UserName"]) |
| 53 | + path = "./pic" |
| 54 | + if not os.path.exists(path): |
| 55 | + os.makedirs(path) |
| 56 | + try: |
| 57 | + file_name = path + os.sep + friend['NickName']+"("+friend['RemarkName']+").jpg" |
| 58 | + with open(file_name, 'wb') as f: |
| 59 | + f.write(img) |
| 60 | + except Exception as e: |
| 61 | + print(repr(e)) |
| 62 | + |
| 63 | + # 处理中文字体 |
| 64 | + @staticmethod |
| 65 | + def get_chinese_font(): |
| 66 | + return FontProperties(fname='/System/Library/Fonts/PingFang.ttc') |
| 67 | + |
| 68 | + # 为图表加上数字 |
| 69 | + @staticmethod |
| 70 | + def auto_label(rects): |
| 71 | + for rect in rects: |
| 72 | + height = rect.get_height() |
| 73 | + plt.text(rect.get_x()+rect.get_width()/2.-0.2, 1.03*height, '%s' % float(height)) |
| 74 | + |
| 75 | + # 展示省份柱状图 |
| 76 | + def show(self): |
| 77 | + labels = self.province_dict.keys() |
| 78 | + means = self.province_dict.values() |
| 79 | + index = np.arange(len(labels)) + 1 |
| 80 | + # 方块宽度 |
| 81 | + width = 0.5 |
| 82 | + # 透明度 |
| 83 | + opacity = 0.4 |
| 84 | + fig, ax = plt.subplots() |
| 85 | + rects = ax.bar(index + width, means, width, alpha=opacity, color='blue', label='省份') |
| 86 | + self.auto_label(rects) |
| 87 | + ax.set_ylabel('数量', fontproperties=self.get_chinese_font()) |
| 88 | + ax.set_title('好友省份分布情况', fontproperties=self.get_chinese_font()) |
| 89 | + ax.set_xticks(index + width) |
| 90 | + ax.set_xticklabels(labels, fontproperties=self.get_chinese_font()) |
| 91 | + # 将x轴标签竖列 |
| 92 | + plt.xticks(rotation=90) |
| 93 | + # 设置y轴数值上下限 |
| 94 | + plt.ylim(0, 100) |
| 95 | + plt.tight_layout() |
| 96 | + ax.legend() |
| 97 | + |
| 98 | + fig.tight_layout() |
| 99 | + plt.show() |
| 100 | + |
| 101 | + # 分词 |
| 102 | + @staticmethod |
| 103 | + def split_text(text): |
| 104 | + all_seg = jieba.cut(text, cut_all=False) |
| 105 | + all_word = ' ' |
| 106 | + for seg in all_seg: |
| 107 | + all_word = all_word + seg + ' ' |
| 108 | + |
| 109 | + return all_word |
| 110 | + |
| 111 | + # 作词云 |
| 112 | + def jieba(self, strs): |
| 113 | + text = self.split_text(strs) |
| 114 | + # 设置一个底图 |
| 115 | + alice_mask = np.array(Image.open('./alice.png')) |
| 116 | + wordcloud = WordCloud(background_color='white', |
| 117 | + mask=alice_mask, |
| 118 | + max_words=1000, |
| 119 | + # 如果不设置中文字体,可能会出现乱码 |
| 120 | + font_path='/System/Library/Fonts/PingFang.ttc') |
| 121 | + myword = wordcloud.generate(str(text)) |
| 122 | + # 展示词云图 |
| 123 | + plt.imshow(myword) |
| 124 | + plt.axis("off") |
| 125 | + plt.show() |
| 126 | + |
| 127 | + # 保存词云图 |
| 128 | + wordcloud.to_file('./alice_word.png') |
| 129 | + |
| 130 | + # 判断中文 |
| 131 | + @staticmethod |
| 132 | + def judge_chinese(word): |
| 133 | + cout1 = 0 |
| 134 | + for char in word: |
| 135 | + if ord(char) not in (97, 122) and ord(char) not in (65, 90): |
| 136 | + cout1 += 1 |
| 137 | + if cout1 == len(word): |
| 138 | + return word |
| 139 | + |
| 140 | + # 处理签名,并生成词云 |
| 141 | + def sign(self): |
| 142 | + sign = [] |
| 143 | + for f in self.friends: |
| 144 | + sign.append(f['Signature']) |
| 145 | + |
| 146 | + strs = '' |
| 147 | + for word in sign[0:]: |
| 148 | + if self.judge_chinese(word) is not None: |
| 149 | + strs += word |
| 150 | + |
| 151 | + self.jieba(strs) |
| 152 | + |
| 153 | + # 主函数 |
| 154 | + def main(self): |
| 155 | + self.login() |
| 156 | + self.crawl() |
| 157 | + self.show() |
| 158 | + self.sign() |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == '__main__': |
| 162 | + wxfriends = WxFriends() |
| 163 | + wxfriends.main() |
0 commit comments