用Python玩转微信

1. 安装相应的库

pip install itchat

pip install echarts-python

2. 实现登陆状态保存:

import itchat

itchat.auto_login(hotReload=True)

itchat.dump_login_status()

这样你就可以保持一段时间登录状态,而不用每次运行代码都要扫码登录了!

首次登录,程序会弹出一个二维码图片窗口,用微信手机客户端扫码就可以登录了!

实现1 好友个性签名词云

实现2 好友性别分布饼图展示


参考资料:https://www.cnblogs.com/feixuelove1009/p/6950102.html


###code 
import itchat

import matplotlib.pyplot as plt

from wordcloud import WordCloud, ImageColorGenerator

import PIL.Image as Image

import os

import numpy as np

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]['NickName']), '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, "mask.png")))

    signature_list = []

    for friend in friends:

        signature = friend["Signature"].strip()

        signature = re.sub("", "", 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"D:\pyweixin\simkai.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"))

    #toUserName : 发送对象, 如果留空, 将发送给自己.

    itchat.send_image("wechat_cloud.png", toUserName=friends[0]['NickName'])

itchat.auto_login(hotReload=True)

itchat.dump_login_status()

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

#echart_pie(friends)

word_cloud(friends)

你可能感兴趣的:(用Python玩转微信)