原文连接: https://mp.weixin.qq.com/s/4EXgR4GkriTnAzVxluJxmg
「itchat」一个开源的微信我的接口,今天咱们就用itchat爬取微信好友信息,无图言虚空
三张图分别是「微信好友头像拼接图」、「性别统计图」、「个性签名统计图」 git
「微信好友头像拼接图」 github
「性别统计图」 微信
「个性签名统计图」 dom
pip3 install itchat
主要用到的方法: itchat.login()
微信扫描二维码登陆 itchat.get_friends()
返回完整的好友列表,每一个好友为一个字典, 其中第一项为本人的帐号信息,传入update=True
, 将更新好友列表并返回, get_friends(update=True)
itchat.get_head_img(userName="")
根据userName
获取好友头像字体
获取好友信息,get_head_img
拿到每一个好友的头像,保存文件,将头像缩小拼接至一张大图。
先获取好友头像:spa
def headImg(): itchat.login() friends = itchat.get_friends(update=True) # itchat.get_head_img() 获取到头像二进制,并写入文件,保存每张头像 for count, f in enumerate(friends): # 根据userName获取头像 img = itchat.get_head_img(userName=f["UserName"]) imgFile = open("img/" + str(count) + ".jpg", "wb") imgFile.write(img) imgFile.close()
这里须要提早在同目录下新建了文件夹img
,不然会报No such file or directory
错误,img
用于保存头像图片,遍历好友列表,根据下标count
命名头像,到这里能够看到文件夹里已经保存了全部好友的头像。 code
接下来就是对头像进行拼接 orm
遍历文件夹的图片,random.shuffle(imgs)
将图片顺序打乱blog
用640*640的大图来平均分每一张头像,计算出每张正方形小图的长宽,压缩头像,拼接图片,一行排满,换行拼接,好友头像多的话,能够适当增长大图的面积,具体代码以下:接口
def createImg(): x = 0 y = 0 imgs = os.listdir("img") random.shuffle(imgs) # 建立640*640的图片用于填充各小图片 newImg = Image.new('RGBA', (640, 640)) # 以640*640来拼接图片,math.sqrt()开平方根计算每张小图片的宽高, width = int(math.sqrt(640 * 640 / len(imgs))) # 每行图片数 numLine = int(640 / width) for i in imgs: img = Image.open("img/" + i) # 缩小图片 img = img.resize((width, width), Image.ANTIALIAS) # 拼接图片,一行排满,换行拼接 newImg.paste(img, (x * width, y * width)) x += 1 if x >= numLine: x = 0 y += 1 newImg.save("all.png")
好友头像图成型,头像是随机打乱拼接的
一样itchat.login()
登陆获取好友信息,根据Sex
字段判断性别,1 表明男性(man),2 表明女性(women),3 未知(unknown)
def getSex(): itchat.login() friends = itchat.get_friends(update=True) 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()
性别统计柱状图
获取好友信息,Signature
字段是好友的签名,将个性签名保存到.txt文件,部分签名里有表情之类的会变成emoji 类的词,将这些还有特殊符号的替换掉。
def getSignature(): 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")
sign.txt
文件里写入了全部好友的个性签名,使用wordcloud包生成词云图,pip install wordcloud
一样能够采用jieba
分词生成词图,不使用分词的话就是句子展现,使用jieba
分词的话能够适当把max_font_size
属性调大,好比100。
须要注意的是运行不要在虚拟环境下,deactivate
退出虚拟环境再跑,详细代码以下:
# 生成词云图 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') # 把词云保存下
句子图
使用jieba
分词产生的词云图
看来,「努力」 「生活」 仍是很重要的
itchat 除了以上的信息,还有省市区等等信息均可以抓取,另外还能够实现机器人自动聊天等功能,这里就不一一律述了。
最后附上github地址:https://github.com/taixiang/itchat_wechat
欢迎关注个人博客:https://blog.manjiexiang.cn/
欢迎关注微信号:春风十里不如认识你