栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

itchat

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

itchat

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


createImg.jpg

getsex.png

signature.png

import itchatimport osimport PIL.Image as Imagefrom os import listdirimport mathimport matplotlib.pyplot as pltimport randomfrom wordcloud import WordCloudimport reif __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("1fd+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:WindowsFonts下,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")



作者:GHope
链接:https://www.jianshu.com/p/0664da7ae037


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/222043.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号