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)/total100))
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\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()
<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, (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')
<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("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')