
将使用itchat抓取微信数据,并对获取到的数据进行全面分析,包含好友性别、地理位置分布、个性签名等进行分析,分析到你怀疑人生。找到为什么单身的原因。
一、itchat简单介绍
本文章用的主角库就是itchat。
itchat是一个开源的微信个人号接口,使用python调用微信从未如此简单。使用不到三十行的代码,你就可以完成一个能够处理所有信息的微信机器人。
本文章使用itchat分析一波微信好友
安装
pip install itchat基本的是使用方法本文章就不做介绍了,itchat的使用不是文章的主要内容。
正题开始
二、目的
itchat抓取微信数据,并进行数据分析,数据可视化显示。
三、获取数据
使用itchat中的 get_friends 方法获取微信所有好友数据
def get_friends():
'''
获取数据
'''
itchat.auto_login()
friends = itchat.get_friends(update=True)
return friends四、处理数据
处理获取到的数据,提前有用的数据
def parse_data(data):
'''
提取有用的数据
该方法获取好友基本信息,且保存好友头像
'''
friends = []
base_path = './headImages'
if not os.path.exists(base_path):
os.mkdir(base_path)
for item in data:
img_data = itchat.get_head_img(userName=item['UserName'])
img_name = item['RemarkName'] if item['RemarkName'] != '' else item['NickName']
img_name = re.sub("[\!\%\[\]\,\。\(\*\-\☻\*\)]", "", img_name)#剔除特殊字符 防止路径os报错
img_file = os.path.join(base_path, img_name+'.jpg')
print(img_file)
with open(img_file, 'wb') as f:
f.write(img_data)
friend = {
'NickName': item['NickName'],
'RemarkName': item['RemarkName'],
'Sex': item['Sex'],
'Province': item['Province'],
'City': item['City'],
'Signature':item['Signature'].replace('\n',' ').replace(',', ' '),
'StarFriend': item['StarFriend'],
'ContactFlag': item['ContactFlag']
}
print(friend)
friends.append(friend)
return friends五、保存数据
将提取的到的数据保存成json文件,方便后面数据分析使用。
def write_data(param):
'''
将数据写入文件中
'''
with open('./wx_friends.json', 'w', encoding='utf-8') as f:
try:
job_json = json.dumps(param, indent=4, ensure_ascii=False)
f.write(job_json )
except Exception as e:
print('-----')
print(e)
pass六、分析数据,数据可视化
使用pyechart分析微信好友数据。
pyechart安装
pip install pyecharts1、好友性别比例分析
def friends_sex(data):
'''
好友性别比列分析
'''
attr = ['male', 'woman', 'other']
male = 0
woman =0
unknown = 0
for item in data:
if item['Sex'] == 0:
unknown += 1
elif item['Sex'] == 1:
male += 1
else:
woman += 1
value = [male, woman, unknown]
pie = Pie(
'好友性格比例',
'好友总人数:%d'%len(data),
title_pos ='center'
)
pie.add(
'',
attr,
value,
radius=[30,75],
rosetype='area',
is_label_show=True,
is_legend_show=True,
legend_top='bottom'
)
pie.render('好友性别比例.html')2、好友位置分布
def friends_address(data):
cities = []
for item in data:
if item['City'] != '':
cities.append(item['City'])
data = Counter(cities).most_common()
print(data)
geo = Geo(
'好友位置分布',
'',
title_color="#fff",
title_pos="center",
width=1200,
height=600,
background_color="#404a59",
)
attr, value = geo.cast(data)
geo.add(
"",
attr,
value,
visual_range=[0, 200],
maptype='china',
visual_text_color="#fff",
symbol_size=15,
is_visualmap=True,
)
geo.render('好友位置分布.html')3、个人签名词云
def signature_cloud(data):
'''
个人签名词云
'''
signature = []
for item in data:
if item['Signature'] != '':
signature.append(item['Signature'])
print(signature)
data = jieba.cut(str(signature), cut_all=False)
words = ' '.join(data)
print(words)
stopwords = STOPWORDS.copy()
stopwords.add('span')
stopwords.add('class')
stopwords.add('emoji')
stopwords.add('emoji1f42c')
stopwords.add('emoji1f388')
stopwords.add('emoji1f525')
stopwords.add('emoji1f33c')
stopwords.add('emoji1f483')
stopwords.add('emoji2764')
mask = np.array(Image.open('./love.png'))
wc = WordCloud(
width=1024,
height=768,
background_color='#fff',
mask=mask,
font_path='C:\Windows\Fonts\AdobeFangsongStd-Regular.otf',
stopwords=stopwords,
max_font_size=400,
random_state=50
)
wc.generate_from_text(words)
# 保存结果到本地
wc.to_file('个性签名词云图.jpg')评论
暂无评论
评论(0)