程序员文章、书籍推荐和程序员创业信息与资源分享平台

网站首页 > 技术文章 正文

用python分析你的朋友圈,很好玩~

hfteth 2025-07-06 12:17:00 技术文章 2 ℃

设计喵的内心OS:我一脸懵逼点进来,一脸懵逼走出去,你说了什么?

事情是这么来的,我看到有朋友在做朋友圈的电子书,也想自己研究一下,发现原来有个itchart这个微信第三方API可以读取微信数据,抱着好奇的想法尝试了以下,果然非常好玩。

程序结构:

getFriendsList.py这个类用来爬取好友信息并保存到指定文件

import itchat
import os
import time

basepath = os.path.dirname(os.path.realpath(__file__))
download_path = basepath+'/downloads'+ '/'

# 调用itchat接口登录并拉取数据
itchat.login
friends = itchat.get_friends(update=True)[0:]

fmt='%Y%m%d%H%M%S' #定义时间显示格式
Date=time.strftime(fmt,time.localtime(time.time))

download_file_name = 'friendsList_'+friends[0]['NickName']
+ '_' + Date + '.txt'

f = open(download_path+download_file_name,'wb')
print(download_path+download_file_name)

for i in friends[1:]:
friend = (str(i) + "\n").encode(encoding='gb18030')
# print(str(i))
f.write(friend)

f.close

analyse.py这个类根据下载的好友数据分析好友信息

import re
import os
import time

source_file = "friendslist_阿西UED_20180105191247.txt"

basepath = os.path.dirname(os.path.realpath(__file__))
download_file = basepath+'/downloads/'+ source_file

fs_str = ''
with open(download_file,'rb') as f:
fs_str = f.read.decode('gb18030')

friends = fs_str.split('\n')

# 初始化计数器
male = female = other = 0
# 所有省份
Provinces_list =
#friends[0]是自己的信息,所以要从friends[1]开始
for i in friends:
if i.__len__>0:
i = i.replace('<ContactList: [','"<ContactList: [')
i = i.replace(']>',']>"')
friend = eval(i)

# 统计性别
sex = friend["Sex"]
if sex == 1:
male += 1
exit
elif sex ==2:
female += 1
else:
other+=1

# 统计地区
Province = friend["Province"]
Provinces_list.append(Province)


#计算朋友总数
total = len(friends)
#打印出自己的好友性别比例
print("总好友数: %d" % total + "\n" +
"男性好友: %d 个,占比 %.2f%%" % (male,(float(male)
/total*100)) + "\n" +
"女性好友: %d 个,占比 %.2f%%" % (female,(float
(female) / total * 100)) + "\n" +
"不明性别好友: %d 个,占比 %.2f%%" % (other,
(float(other) / total * 100)))

Provinces_set = set(Provinces_list)
Provinces_dict = {}
for i in Provinces_set:
Provinces_dict[i] = Provinces_list.count(i)
# 对省份字典按value排序
Provinces_dict = sorted(Provinces_dict.items,
key=lambda asd:asd[1],reverse=True)

print("==========人数排名前10地区如下============")
top = 0
for k,v in Provinces_dict:
if top<10:
print("%s : %d 个,占比 : %.2f%%" %
(k,v,float(v)/total*100))
top+=1

输出结果:

总好友数: 1324

男性好友: 680 个,占比 51.36%

女性好友: 592 个,占比 44.71%

不明性别好友: 51 个,占比 3.85%

=======人数排名前10地区如下========

未知 : 258 个,占比 : 19.49%

北京 : 211 个,占比 : 15.94%

上海 : 198 个,占比 : 14.95%

广东 : 173 个,占比 : 13.07%

河南 : 68 个,占比 : 5.14%

浙江 : 63 个,占比 : 4.76%

江苏 : 51 个,占比 : 3.85%

四川 : 24 个,占比 : 1.81%

山东 : 21 个,占比 : 1.59%

河北 : 19 个,占比 : 1.44%

wordCloud.py根据签名生成词云

词云来自全部好友的签名,把签名收集在一起拼接字符串然后分词再拼成一张图片。重复越多的字越大。

# -*- coding:UTF-8 -*-
import re
import os
import time

base_pic = "alice_coloring.jpg"
source_file = "friendsList_阿西UED_20180105191247.txt"

basepath = os.path.dirname(os.path.realpath(__file__))
download_file = basepath+'/downloads/'+ source_file

fs_str = ''
with open(download_file,'rb') as f:
fs_str = f.read.decode('gb18030')

friends = fs_str.split('\n')


siglist =
for i in friends:
if i.__len__>0:
i = i.replace('<ContactList: [','"
<ContactList: [')
i = i.replace(']>',']>"')
friend = eval(i)
# print(friend)
# print(friend["Signature"])
signature = friend["Signature"].strip
.replace("span","").replace("class","")
.replace("emoji","")
rep = re.compile("1f\d+\w*|[<>/=]")
signature = rep.sub("",signature)
siglist.append(signature)
text = "".join(siglist)

import jieba
wordlist = jieba.cut(text,cut_all=True)
word_space_split = " ".join(wordlist).replace("\n","")

print(word_space_split)



import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
import numpy as np
import PIL.Image as Image

coloring = np.array(Image.open(base_pic))
my_wordcloud = WordCloud(background_color="white",
max_words=2000,mask=coloring,max_font_size=60,
random_state=42,scale=2,
font_path="fonts/STHeiti Light.ttc")
.generate(word_space_split)

image_colors = ImageColorGenerator(coloring)
plt.imshow(my_wordcloud.recolor(color_func=image_colors))
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show

词云的效果:

python还有其他比较好玩的东西,比如小程序辅助就是python开发的我经过测试已经被官方黑名单了,🤭,不过用来自娱自乐很奈斯。

还能分析你朋友圈全部的内容,图片、文字哪些做微信电子书就是用这个原理做的爬虫。

原文链接是我用js实现的微信跳转支付宝demo有兴趣的可以体验一下,明天文章发教程。

感觉要转行做开发了的感觉~ 😂

Tags:

最近发表
标签列表