Python与微信——itchat包

摘要:
获取好友列表=ithat.Get_friends(update=True)[0:%s'%(msg['Text'])ifname==“main”:ithat.auto_Login()#获取用户名myUserName=ithat_Get_friend(update=True)[0]['UserName']ithat.run()<

目录

itchat

一安装itchat

pip install itchat pip install echarts-python

二登陆并向文件传输助手发消息

``` import itchat 登录

itchat.login()

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

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

<h4 class='title'>二微信好友性别比例</h4>
获取好友列表

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

<h4 class='title'>三微信设置自动回复</h4>

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 '%(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(msg['CreateTime'])),
msg['User']['NickName'],
msg['Text']
),
'filehelper')
# 回复给好友
return u'[自动回复]您好,我现在有事不在,一会再和您联系。 已经收到您的的信息:%s ' % (msg['Text'])

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

<h4 class='title'>四好友头像拼接</h4>

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, (x
each_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')

<h4 class='title'>五微信个性签名词云</h4>

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("1fd.+")
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')

免责声明:文章转载自《Python与微信——itchat包》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇kali 系统 wifi密码破解笔记C#自定义工业控件开发下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python之路 django基础

Python的WEB框架有Django、Tornado、Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM、模型绑定、模板引擎、缓存、Session等诸多功能。 一、基本配置 1、创建Django程序 终端命令:django-admin startproject sitename IDE创建Django程序时,本质...

机器学习笔记14-----SVM实践和分类器的性能的评价指标(了解python画图的技巧)

1.主要内容 2.SVM的应用 (1)利用SVM处理分类问题 分类器的性能的评价指标: 应用案例: accuracy=3/6=0.5 precision=3/5=0.6 recall=3/4=0.75 3.代码示例 (1)鸢尾花SVM案例 #!/usr/bin/python # -*- coding:utf-8 -*- import nump...

freemark使用总结

1.下拉框中使用三元表达式: <option value="1类" ${(bean.col5!?string="1类")?string('selected','')}>1类</option> 2.ftl页面中保留小数 <td><input type="text" class="form-control" valu...

My97日期控件My97DatePicker使用(备忘)

前言: 最近老是找不到资料,痛之又痛的情况下,决定好好将所有涉及到的东西通通做个备忘记录。 参考网址: 1、http://www.my97.net/dp/demo/index.htm 2、http://www.mysuc.com/test/My97DatePicker/#m246 使用备忘—— 日期:2010-10-30 版本:4.7 功能:两个日期文本框...

python批量操作Linux服务器脚本,ssh密码登录(执行命令、上传、下载)(一)

 1 #-*- coding: utf-8 -*- 2 #批量操作linux服务器(执行命令,上传,下载) 3 #!/usr/bin/python 4 import paramiko 5 import datetime 6 import os 7 import threading 8 def ssh2(ip,username,passwd,cmd): 9...

vue从一个页面跳转到另一个页面并携带参数

1.需求: 点击商场跳转到商业体列表 解决方案: 元页面: a标签中添加跳转函数 <a class="orderBtn1 sIRicon2" href="javascript:void(0);" @click="toMallInfo('M000989')"><i class="sIRicon"></i>商场<...