天天看點

python學習-微信好友簽名詞雲展示一:介紹源代碼:

微信好友簽名詞雲展示

一:介紹

1.主要是按照百度上的流程一步一個腳印

2.做好相關包安裝和配置

3.複制粘貼代碼,然後進行修改

4.一般都是修改檔案放置的位置

5.此處主要是圖檔所處的位置,事先下載下傳好圖檔,并放到與代碼一個檔案夾下。

6.生成的圖檔也會在此檔案夾下

源代碼:

#!/usr/bin/env python
# -*- coding:utf-8 -*- 
import itchat
import re
import jieba

def echart_pie(friends):
    total = len(friends) - 1
    male = female = other = 0
 
    for friend in friends[1:]:
        sex = friend["Sex"]
        if sex == 1:
            male += 1
        elif sex == 2:
            female += 1
        else:
            other += 1
    from echarts import Echart, Legend, Pie
    chart = Echart('%s的微信好友性别比例' % (friends[0]['Name']), 'from WeChat')
    chart.use(Pie('WeChat',
                  [{'value': male, 'name': '男性 %.2f%%' % (float(male) / total * 100)},
                   {'value': female, 'name': '女性 %.2f%%' % (float(female) / total * 100)},
                   {'value': other, 'name': '其他 %.2f%%' % (float(other) / total * 100)}],
                  radius=["50%", "70%"]))
    chart.use(Legend(["male", "female", "other"]))
    del chart.json["xAxis"]
    del chart.json["yAxis"]
    chart.plot()
 
 
def word_cloud(friends):
    import matplotlib.pyplot as plt
    from wordcloud import WordCloud, ImageColorGenerator
    import PIL.Image as Image
    import os
    import numpy as np
    d= os.path.dirname(__file__)
    my_coloring = np.array(Image.open(os.path.join(d, "2.png")))
    signature_list = []
    for friend in friends:
        signature = friend["Signature"].strip()
        signature = re.sub("<span.*>", "", signature)
        signature_list.append(signature)
    raw_signature_string = ''.join(signature_list)
    text = jieba.cut(raw_signature_string, cut_all=True)
    target_signatur_string = ' '.join(text)
 
    my_wordcloud = WordCloud(background_color="white", max_words=2000, mask=my_coloring,
                             max_font_size=40, random_state=42,
                             font_path=r"C:\Windows\Fonts\simhei.ttf").generate(target_signatur_string)
    image_colors = ImageColorGenerator(my_coloring)
    plt.imshow(my_wordcloud.recolor(color_func=image_colors))
    plt.imshow(my_wordcloud)
    plt.axis("off")
    plt.show()
    # 儲存圖檔 并發送到手機
    my_wordcloud.to_file(os.path.join(d, "wechat_cloud.png"))
    itchat.send_image("wechat_cloud.png", 'filehelper')
 
 
itchat.auto_login(hotReload=True)
itchat.dump_login_status()
 
friends = itchat.get_friends(update=True)[:]
 
# echart_pie(friends)
 
word_cloud(friends)
           

參考:http://www.cnblogs.com/feixuelove1009/p/6950102.html