爬取微信好友的部分资料,并将所有人的个性签名制成词云图,哈哈~~~~

import itchat
itchat.login()
friends = itchat.get_friends(update=True)[0:]


male = female = other = 0
for i in friends[1:]:
    sex = i["Sex"]
    if sex == 1:
        male += 1
    elif sex == 2:
        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

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
siglist = []
for i in friends:
    signature = i["Signature"].strip().replace("span","").replace("class","").replace("emoji","")
    rep = re.compile("lf\d+\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)

import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
import numpy as np
import PIL.Image as Image

coloring = np.array(Image.open("1.jpg"))
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()

最后的效果图如下所示:

爬取微信好友的部分资料,并将所有人的个性签名制成词云图,哈哈~~~~_第1张图片

你可能感兴趣的:(爬虫)