您的位置:首页 > 编程语言 > Python开发

Python项目:用所有朋友微信头像做云图图

2018-01-29 20:27 841 查看
来自:https://mp.weixin.qq.com/s/mW7PTofuCOQrW5e34Ei2Pw
#2018-01-28 19:57:38 January Sunday the 04 week, the 028 day SZ SSMR
################获取微信朋友所有信息#################
#登陆网页版微信
import itchat
itchat.login()
friends = itchat.get_friends(update = True)[0:]

#2018-01-28 19:57:34 January Sunday the 04 week, the 028 day SZ SSMR
#爬朋友性别比例。
male = female = other = 0
#friends[0]是自己信息,爬朋友,所以从friends[1]开始
for i in friends[1:]:
sex = i["Sex"]
if sex == 1:
male = male + 1
elif sex == -1:
female +=1
else:
other +=1
#计算朋友总数
total = len(friends[1:])
print("汉子比例:%.2f%%"%(float(male)/total * 100) + "\n" + "妹子比例:%.2f%%"%(float(female)/total * 100) + "\n" + "不男不女比例:%.2f%%"%(float(other)/total * 100))

##########分析好友城市分布###################
#定义函数,爬取不同变量
def get_var(var):
variable = []
for i in friends:
value = i[var]
variable.append(value)
return variable

#调用上面函数得到不同的变量信息,并且保存到CSV文件里,保存到桌面
NickName = get_var("NickName")
Sex = get_var("Sex")
Province = get_var("Province")
City = get_var("City")
Signature = get_var("Signature")
from pandas import DataFrame
data = {'NickName':NickName, 'Sex':Sex, 'Province':Province, 'City':City, 'Signature':Signature}
frame = DataFrame(data)
frame.to_csv('data.csv', index = True)

###############微信好友个性签名的自定义云图#################
import re 		#re正则表达式模块
siglist = []
for i in friends:
#调取每个签名,并且删除左右空格,删除span,class,emoji等会出现的各种表情包
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)		#把列表转换为文本格式

#结巴分词包把上面的text导入
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
coloring = np.array(Image.open("users/Apple/Desktop/wechat.jpg"))
my_wordcloud = WordCloud(background_color = "white", max_words = 2000, mask = coloring, max_font_size = 60,random_state = 42,
scale = 2, font_path = "Libray/Fonts/Microsoft/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()


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐