Pythonpython自学

itchat

2018-08-09  本文已影响60人  GHope

itchat的基本使用:
(需要注意的一点是拼接好友头像导包的时候python3以上版本的需要导入Pillow,以下的导入PIL)


createImg.jpg getsex.png signature.png
import itchat
import os
import PIL.Image as Image
from os import listdir
import math
import matplotlib.pyplot as plt
import random
from wordcloud import WordCloud
import re

if __name__ == '__main__':
    # 登录
    itchat.auto_login(hotReload=True)

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

    # 发送信息
    # for item friends:
    #     print(item)
    #     print(item['NickName'])
    # number = 500
    # while number:
    #     itchat.send_msg('', friends[20]['UserName'])
    #     number -= 1

    # 爬取拼接图片
    # user = friends[0]["UserName"]
    #
    # print(user)
    #
    # os.mkdir(user)
    #
    # num = 0
    #
    # for i in friends:
    #     img = itchat.get_head_img(userName=i["UserName"])
    #
    #     fileImage = open(user + "/" + str(num) + ".jpg", 'wb')
    #
    #     fileImage.write(img)
    #
    #     fileImage.close()
    #
    #     num += 1
    #
    # pics = listdir(user)
    #
    # numPic = len(pics)
    #
    # print(numPic)
    #
    # eachsize = int(math.sqrt(float(640 * 640) / numPic))
    #
    # print(eachsize)
    #
    # numline = int(640 / eachsize)
    #
    # toImage = Image.new('RGB', (640, 640))
    #
    # print(numline)
    #
    # x = 0
    #
    # y = 0
    #
    # for i in pics:
    #
    #     try:
    #
    #         # 打开图片
    #
    #         img = Image.open(user + "/" + i)
    #
    #     except IOError:
    #
    #         print("Error: 没有找到文件或读取文件失败")
    #
    #     else:
    #
    #         # 缩小图片
    #
    #         img = img.resize((eachsize, eachsize), Image.ANTIALIAS)
    #
    #         # 拼接图片
    #
    #         toImage.paste(img, (x * eachsize, y * eachsize))
    #
    #         x += 1
    #
    #         if x == numline:
    #             x = 0
    #
    #             y += 1
    #
    # toImage.save(user + ".jpg")
    #
    # itchat.send_image(user + ".jpg", 'filehelper')

    # 性别统计
    # sex = dict()
    # for f in friends:
    #     if f["Sex"] == 1:  # 男
    #         sex["man"] = sex.get("man", 0) + 1
    #     elif f["Sex"] == 2: #女
    #         sex["women"] = sex.get("women", 0) + 1
    #     else: #未知
    #         sex["unknown"] = sex.get("unknown", 0) + 1
    # # 柱状图展示
    # for i, key in enumerate(sex):
    #     plt.bar(key, sex[key])
    # plt.show()

    # 获取词云
    itchat.login()
    friends = itchat.get_friends(update=True)
    file = open('sign.txt', 'a', encoding='utf-8')
    for f in friends:
        signature = f["Signature"].strip().replace("emoji", "").replace("span", "").replace("class", "")
        rec = re.compile("1f\d+\w*|[<>/=]")
        signature = rec.sub("", signature)
        file.write(signature + "\n")
        
        
    # 生成词云图
    def create_word_cloud(filename):
        # 读取文件内容
        text = open("{}.txt".format(filename), encoding='utf-8').read()
    
        # 注释部分采用结巴分词
        # wordlist = jieba.cut(text, cut_all=True)
        # wl = " ".join(wordlist)
    
        # 设置词云
        wc = WordCloud(
            # 设置背景颜色
            background_color="white",
            # 设置最大显示的词云数
            max_words=2000,
            # 这种字体都在电脑字体中,window在C:\Windows\Fonts\下,mac下可选/System/Library/Fonts/PingFang.ttc 字体
            font_path='C:\\Windows\\Fonts\\simfang.ttf',
            height=500,
            width=500,
            # 设置字体最大值
            max_font_size=60,
            # 设置有多少种随机生成状态,即有多少种配色方案
            random_state=30,
        )
    
        myword = wc.generate(text)  # 生成词云 如果用结巴分词的话,使用wl 取代 text, 生成词云图
        # 展示词云图
        plt.imshow(myword)
        plt.axis("off")
        plt.show()
        wc.to_file('signature.png')  # 把词云保存下


    create_word_cloud("sign")
上一篇下一篇

猜你喜欢

热点阅读