设计喵的内心OS:我一脸懵逼点进来,一脸懵逼走出去,你说了什么?
程序结构:
getFriendsList.py这个类用来爬取好友信息并保存到指定文件
importitchat
importos
importtime
basepath = os.path.dirname(os.path.realpath(__file__))
download_path = basepath+'/downloads'+'/'
# 调用itchat接口登录并拉取数据
itchat.login()
friends = itchat.get_friends(update=True)[:]
fmt='%Y%m%d%H%M%S'#定义时间显示格式
Date=time.strftime(fmt,time.localtime(time.time()))
download_file_name ='friendsList_'+friends[]['NickName']
+'_'+ Date +'.txt'
f =open(download_path+download_file_name,'wb')
print(download_path+download_file_name)
foriinfriends[1:]:
friend = (str(i) +"\n").encode(encoding='gb18030')
# print(str(i))
f.write(friend)
f.close()
analyse.py这个类根据下载的好友数据分析好友信息
importre
importos
importtime
source_file ="friendslist_阿西UED_20180105191247.txt"
basepath = os.path.dirname(os.path.realpath(__file__))
download_file = basepath+'/downloads/'+ source_file
fs_str =''
withopen(download_file,'rb')asf:
fs_str = f.read().decode('gb18030')
friends = fs_str.split('\n')
# 初始化计数器
male = female = other =
# 所有省份
Provinces_list = []
#friends[0]是自己的信息,所以要从friends[1]开始
foriinfriends:
ifi.__len__()>:
i = i.replace('
i = i.replace(']>',']>"')
friend =eval(i)
# 统计性别
sex = friend["Sex"]
ifsex ==1:
male +=1
exit
elifsex ==2:
female +=1
else:
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 = {}
foriinProvinces_set:
Provinces_dict[i] = Provinces_list.count(i)
# 对省份字典按value排序
Provinces_dict =sorted(Provinces_dict.items(),
key=lambdaasd:asd[1],reverse=True)
print("==========人数排名前10地区如下============")
top =
fork,vinProvinces_dict:
iftop
print("%s : %d 个,占比 : %.2f%%"%
(k,v,float(v)/total*100))
top+=1
输出结果:
总好友数:1324
男性好友:680 个,占比 51.36%
女性好友:592 个,占比 44.71%
不明性别好友:51 个,占比 3.85%
=======人数排名前10地区如下========
未知: 258 个,占比 : 19.49%
北京 : 211 个,占比 : 15.94%
上海 : 198 个,占比 : 14.95%
广东 : 173 个,占比 : 13.07%
河南 : 68 个,占比 : 5.14%
浙江 : 63 个,占比 : 4.76%
江苏 : 51 个,占比 : 3.85%
四川 : 24 个,占比 : 1.81%
山东 : 21 个,占比 : 1.59%
河北 : 19 个,占比 : 1.44%
wordCloud.py根据签名生成词云
词云来自全部好友的签名,把签名收集在一起拼接字符串然后分词再拼成一张图片。重复越多的字越大。
# -*- coding:UTF-8 -*-
importre
importos
importtime
base_pic ="alice_coloring.jpg"
source_file ="friendsList_阿西UED_20180105191247.txt"
basepath = os.path.dirname(os.path.realpath(__file__))
download_file = basepath+'/downloads/'+ source_file
fs_str =''
withopen(download_file,'rb')asf:
fs_str = f.read().decode('gb18030')
friends = fs_str.split('\n')
siglist = []
foriinfriends:
ifi.__len__()>:
i = i.replace('
i = i.replace(']>',']>"')
friend =eval(i)
# print(friend)
# print(friend["Signature"])
signature = friend["Signature"].strip()
.replace("span","").replace("class","")
.replace("emoji","")
rep = re.compile("1f\d+\w*[/=]")
signature = rep.sub("",signature)
siglist.append(signature)
text ="".join(siglist)
importjieba
wordlist = jieba.cut(text,cut_all=True)
word_space_split =" ".join(wordlist).replace("\n","")
print(word_space_split)
importmatplotlib.pyplotasplt
fromwordcloudimportWordCloud, ImageColorGenerator
importnumpyasnp
importPIL.ImageasImage
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="fonts/STHeiti Light.ttc")
.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()
词云的效果:
python还有其他比较好玩的东西,比如小程序辅助就是python开发的我经过测试已经被官方黑名单了,
领取专属 10元无门槛券
私享最新 技术干货