Dowemo
0 0 0 0

in zhihu, I've seen someone recommended itchart this wechat interface, trying to try the following, and it's very fun.
official link: http://itchat.readthedocs.io/zh/latest/#itchat

directory structure

这里写图片描述
get_info class for buddy information and save to the specified file

import itchat
import os
import timebasepath = os.path.dirname(os.path.realpath(__file__))
download_path = basepath+'downloads'+ ''# 调用itchat接口登录并拉取数据itchat.login()
friends = itchat.get_friends(update=True)[0:]
fmt='%Y%m%d%H%M%S'#定义时间显示格式Date=time.strftime(fmt,time.localtime(time.time()))
download_file_name = 'friendslist_'+friends[0]['NickName']+ '_' + Date + '.txt'f = open(download_path+download_file_name,'wb')print(download_path+download_file_name)for i in friends[1:]:
 friend = (str(i) + "n").encode(encoding='gb18030')
 # print(str(i)) f.write(friend)
f.close()

analyse. py this class analyzes friends information based on downloaded friends

# Author:Jason.wangimport re
import os
import timebase_pic = "C:/Users/Think/Pictures/Saved Pictures/beb28c538ac52a91.jpg"source_file = "friendslist_雨佳Clara_20170724223344.txt"source_file = "friendslist_say_20170724132202.txt"basepath = os.path.dirname(os.path.realpath(__file__))
download_file = basepath+'downloads'+ source_file
fs_str = ''with open(download_file,'rb') as f:
 fs_str = f.read().decode('gb18030')
friends = fs_str.split('n')# 初始化计数器 male = female = other = 0# 所有省份Provinces_list = []#friends[0]是自己的信息,所以要从friends[1]开始 for i in friends:
 if i.__len__()>0:
 i = i.replace('<ContactList: [','"<ContactList: [')
 i = i.replace(']>',']>"')
 friend = eval(i)
 # 统计性别 sex = friend["Sex"]
 if sex == 1:
 male += 1exit elif sex ==2:
 female += 1else:
 other+=1# 统计地区 Province = friend["Province"]
 Provinces_list.append(Province)#计算朋友总数 total = len(friends)#打印出自己的好友性别比例 print("总好友数: %d" % total + "n" +
 "男性好友: %d 个,占比 %.2f%%" % (male,(float(male)/total*100)) + "n" +
 "女性好友: %d 个,占比 %.2f%%" % (female,(float(female)/total * 100)) + "n" +
 "不明性别好友: %d 个,占比 %.2f%%" % (other,(float(other)/total * 100)))
Provinces_set = set(Provinces_list)
Provinces_dict = {}for i in Provinces_set:
 Provinces_dict[i] = Provinces_list.count(i)# 对省份字典按value排序Provinces_dict = sorted(Provinces_dict.items(),key=lambda asd:asd[1],reverse=True)print("===============人数排名前10地区如下==================")
top = 0for k,v in Provinces_dict:
 if top<10:
 print("%s : %d 个,占比: %.2f%%" % (k,v,float(v)/total*100))
 top+=1

output results:
这里写图片描述

ciyun. py generates based on signature

# -*- coding:UTF-8 -*-# Author:Jason.wangimport re
import os
import timebase_pic = "C:/Users/Think/Pictures/Saved Pictures/beb28c538ac52a91.jpg"source_file = "friendslist_雨佳Clara_20170724223344.txt"# source_file ="friendslist_say_20170724132202.txt"basepath = os.path.dirname(os.path.realpath(__file__))
download_file = basepath+'downloads'+ source_file
fs_str = ''withopen(download_file,'rb') as f:
 fs_str = f.read().decode('gb18030')
friends = fs_str.split('n')
siglist = []for i in friends:
 if i.__len__()>0:
 i = i.replace('<ContactList: [','"<ContactList: [')
 i = i.replace(']>',']>"')
 friend = eval(i)
 # print(friend)# print(friend["Signature"]) signature = friend["Signature"].strip().replace("span","").replace("class","").replace("emoji","")
 rep = re.compile("1fd+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).replace("n","")
print(word_space_split)
import matplotlib.pyplot as pltfrom wordcloud import WordCloud, ImageColorGenerator
import numpy as np
import PIL.Image as Image
coloring = np.array(Image.open(base_pic))
my_wordcloud = WordCloud(background_color="white",max_words=2000,
 mask=coloring,max_font_size=60,random_state=42,scale=2,
 font_path="C:/windows/fonts/SimHei.ttf").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()

effect:
这里写图片描述




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs