Python微信

安装setuptool
安装pip
安装itchat

微信把好友的消息返回给好友

#! /usr/bin/env python
#coding=gbk
import itchat
@itchat.msg_register(itchat.content.TEXT)
def print_content(msg):
    return msg['Text']

itchat.auto_login()
itchat.run()

微信好友男女比例饼状图

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

itchat.auto_login(hotReload=True)
itchat.dump_login_status()

friends = itchat.get_friends(update=True)[:] #获取所有好友的信息,储存在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
# print("男性好友:%.2f%%" % (float(male) / total * 100))
# print("女性好友:%.2f%%" % (float(female) / total * 100))
# print("其他:%.2f%%" % (float(other) / total * 100))

from echarts import Echart, Legend, Pie
chart = Echart(u'%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()

这里写图片描述

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import itchat
import time
itchat.auto_login(hotReload=True)
itchat.dump_login_status()

@itchat.msg_register('Text')
def auto_reply(msg):
    if not msg['FromUserName']==myUserName:
        itchat.send_msg(u"[%s]收到好友@%s的信息:%s\n"%
                       (time.strftime("%y-%m-%d %h-%m-%s",time.locatime(msg['CreateTime'])),
                       msg['User']['NickName'],
                       msg['Text']),'filehelper')
        return u'[自动回复]您好,已经收到您的信息:%s\n我现在有事不在,一会儿再和您联系。'%(msg['Text'])

if __name__=='__main__':
    itchat.auto_login()
    myUserName=itchat.get_friends(update=True)[0]['UserName']
    itchat.run()

微信好友个签高频词云图

#!/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, "3.jpg")))
    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)

这里写图片描述
在安装wordcloud库的时候经常报错,报错wordcloud-1.3.1-cp27-none-win_amd64.whl is not a supported wheel on this platform.发现是文件的命名结构不被平台支持。

import pip; 
print(pip.pep425tags.get_supported())

输入上面代码到shell,获得支持的命名结构。
这里写图片描述
发现还是不行。。。最后安装wordcloud-1.3.1-cp27-none-win32.whl成功了,可我是64位系统啊。。。费解

没有更多推荐了,返回首页