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

来自: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()



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值