python第三方库文件传输助手_Python与微信——itchat包

本文介绍了使用itchat库进行微信自动化操作的四个实例:向文件传输助手发送消息、获取并分析微信好友性别比例、设置自动回复以及拼接好友头像制作词云图。通过这些实例,展示了如何利用Python与微信API交互,进行信息统计和个性化应用。
摘要由CSDN通过智能技术生成

[TOC] ##itchat

一安装itchat
pip install itchat pip install echarts-python
二登陆并向文件传输助手发消息
``` import itchat

登录

itchat.login()

发送消息,filehelper是文件传输助手

itchat.send(u'hello', 'filehelper')

二微信好友性别比例

获取好友列表

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

初始化计数器,有男有女

male = female = other = 0

遍历这个列表,列表里第一位是自己,所以从“自己”之后计算

Sex中1是男士,2是女士

UserName, City, DisplayName, Province, NickName, KeyWord, RemarkName, HeadImgUrl, Alias,Sex

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)/total100)) print("女性好友:%.2f%%"%(float(female)/total100)) print("其他:%.2f%%"%(float(other)/total*100))

三微信设置自动回复

import itchat import time

自动回复

封装好的装饰器,当接收到的消息是Text

@itchat.msg_register('Text') def text_reply(msg): # 当消息不是由自己发出 if not msg['FromUserName'] == myUserName: # 发送一条提示给文件助手 itchat.send_msg(u'[%s]收到好友@%s的信息:%s\n'%(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(msg['CreateTime'])), msg['User']['NickName'], msg['Text'] ), 'filehelper') # 回复给好友 return u'[自动回复]您好,我现在有事不在,一会再和您联系。\n已经收到您的的信息:%s\n' % (msg['Text'])

if name == "main": itchat.auto_login() # 获取自己的UserName myUserName = itchat.get_friends(update=True)[0]['UserName'] itchat.run()

四好友头像拼接

import itchat import math import PIL.Image as PImage import os

Img_Dir = os.path.join(os.path.dirname(file), 'img') all_img_dir = os.path.join(os.path.dirname(os.path.dirname(file)), 'images')

itchat.auto_login() friends = itchat.get_friends(update=True)[0:] print('my friends====', friends) user = friends[0]['UserName']

num = 0 for i in friends: img = itchat.get_head_img(userName=i['UserName']) fileImage = open(os.path.join(Img_Dir, str(num)+".png"), 'wb') fileImage.write(img) fileImage.close() num+=1

ls = os.listdir(Img_Dir) each_size = int(math.sqrt(float(640640)/len(ls))) lines = int(640/each_size) image = PImage.new('RGBA', (640,640)) x = 0 y = 0 for i in range(0, len(ls)+1): try: img = PImage.open(os.path.join(Img_Dir, str(i)+".png")) except IOError: print('Error') else: img = img.resize((each_size, each_size), PImage.ANTIALIAS) image.paste(img, (xeach_size, y*each_size)) x += 1 if x == lines: x = 0 y += 1

img_path = os.path.join(all_img_dir, 'all.png') image.save(img_path) itchat.send_image(img_path, 'filehelper')

五微信个性签名词云

import itchat import re

jieba分词

import jieba

wordcloud词云

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

先登录

itchat.login()

获取好友列表

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

tlist = [] for i in friends: # 获取签名 signature1 = i['Signature'].strip().replace('span', '').replace('class','').replace('emoji','') # 正则过滤emoji表情 rep = re.compile("1f\d.+") signature = rep.sub('', signature1) tlist.append(signature)

拼接字符串

text = ''.join(tlist)

jieba分词

word_list_jieba = jieba.cut(text, cut_all=True) wl_space_split = ' '.join(word_list_jieba)

图片路径

projiect_path = os.path.dirname(os.path.dirname(file)) img_dir = os.path.join(projiect_path, 'images') alice_coloring = np.array(Image.open(os.path.join(img_dir, 'ciyun.jpg')))

选择字体存放路径

my_wordcloud = WordCloud( # 设置背景颜色 background_color='white', max_words=2000, # 词云形状 mask=alice_coloring, # 最大字号 max_font_size=40, random_state=42, # 设置字体,不设置就会乱码 font_path=os.path.join(img_dir, 'simsun.ttc') ).generate(wl_space_split)

image_colors = ImageColorGenerator(alice_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(img_dir, 'myciyun.png')) itchat.send_image(os.path.join(img_dir, 'myciyun.png'), 'filehelper')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值