使用itchat包获取微信好友信息

本文通过Python爬取微信好友信息,分析了好友的性别、地域分布及个性签名,利用数据可视化展示结果,并生成词云。
import itchat
import numpy as np
import pandas as pd
from collections import defaultdict
import re
import jieba
import os
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
import PIL.Image as Image

itchat.login()
friends = itchat.get_friends(update=True)
NickName = friends[0].NickName #获取自己的昵称
if not os.path.exists(NickName):
    os.mkdir(NickName) #为自己创建一个文件夹

file = '\%s' %NickName #刚刚创建的那个文件夹的相对路径
cp = os.getcwd() #当前路径
path = os.path.join(cp+file) #刚刚创建的那个文件夹的绝对路径
os.chdir(path) #切换路径

number_of_friends = len(friends)
df_friends = pd.DataFrame(friends)

def get_count(Sequence):
    counts = defaultdict(int) #初始化一个字典
    for x in Sex:
        counts[x] += 1
    return counts

Sex = df_friends.Sex
Sex_count = get_count(Sex )
Sex_count = Sex.value_counts() #defaultdict(int, {0: 31, 1: 292, 2: 245})
Sex_count.plot(kind = 'bar')
Province = df_friends.Province
Province_count = Province.value_counts()
Province_count = Province_count[Province_count.index!=''] #有一些好友地理信息为空,过滤掉这一部分人。

City = df_friends.City #[(df_friends.Province=='北京') | (df_friends.Province=='四川')]
City_count = City.value_counts()
City_count = City_count[City_count.index!='']

file_name_all = NickName+'_basic_inf.txt'

write_file = open(file_name_all,'w')

write_file.write('你共有%d个好友,其中有%d个男生,%d个女生,%d未显示性别。\n\n' %(number_of_friends, Sex_count[1], Sex_count[2], Sex_count[0])+

                 '你的朋友主要来自省份:%s(%d)、%s(%d)和%s(%d)。\n\n' %(Province_count.index[0],Province_count[0],Province_count.index[1],Province_count[1],Province_count.index[2],Province_count[2])+
                 '主要来自这些城市:%s(%d)、%s(%d)、%s(%d)、%s(%d)、%s(%d)和%s(%d)。'%(City_count.index[0],City_count[0],City_count.index[1],City_count[1],City_count.index[2],City_count[2],City_count.index[3],City_count[3],City_count.index[4],City_count[4],City_count.index[5],City_count[5]))

write_file.close()
Signatures = df_friends.Signature
regex1 = re.compile('<span.*?</span>') #匹配表情
regex2 = re.compile('\s{2,}')#匹配两个以上占位符。
Signatures = [regex2.sub(' ',regex1.sub('',signature,re.S)) for signature in Signatures] #用一个空格替换表情和多个空格。
Signatures = [signature for signature in Signatures if len(signature)>0] #去除空字符串
text = '\n'.join(Signatures)
file_name = NickName+'_wechat_signatures.txt'
with open(file_name,'w',encoding='utf-8') as f:
    f.write(text)
    f.close()
wordlist = jieba.cut(text, cut_all=True)

word_space_split = ' '.join(wordlist)

coloring = np.array(Image.open("F:\duolaameng.jpg")) #词云的背景和颜色。这张图片在本地。

my_wordcloud = WordCloud(background_color="white", max_words=500,
                         mask=coloring, max_font_size=100, random_state=42, scale=2,
                         font_path="C:\Windows\Fonts\STZHONGS.TTF").generate(word_space_split) #生成词云。font_path="C:\Windows\Fonts\msyhl.ttc"指定字体,有些字不能解析中文,这种情况下会出现乱码。

file_name_p = NickName+'.jpg'
my_wordcloud.to_file(file_name_p) #保存图片






 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值