As a chat tool suitable for all ages, WeChat is the network data version of the real circle of friends in our lives. Have we ever thought of organizing and summarizing the data of WeChat, so as to discover the secrets of our circle of friends, making friends, and life The clues of the way? The nicknames, signatures, and regions on WeChat are expressed in words, and they are used as text data in python. By mining and analyzing these text data, we can clearly discover some interesting secrets... if you also hide If you are interested in the messages under the text and symbols, and want to find out about your own circle of friends, then let's take a look at the following operations!
First introduce the tools we use: itchat, itchat is an open source WeChat personal account interface, using it we can easily and quickly access the information in our personal WeChat account.
To use the itchat library, we first install and reference:
pip install itchat
import itchat
Then we log in to our WeChat:
itchat.auto_login
After execution, a QR code image file will be generated in the current directory and automatically pop up. At this time, you can scan the QR code with your mobile phone WeChat. After the mobile phone is confirmed, you can log in to the computer, and the terminal will prompt that the login is successful. The QR code will also disappear automatically.
Many people learn python and after mastering the basic grammar, they don't know where to find cases to get started.
Many people who have done case studies do not know how to learn more advanced knowledge.
So for these three types of people, I will provide you with a good learning platform, free to receive video tutorials, e-books, and the source code of the course!
QQ group: 701698587
However, in the course of my experiment, I encountered the following situation:
I repeated, restarted, and changed the computer. I still failed to log in no matter what the tossing. I browsed many posts on the Internet and found that many people like me are experiencing the problem of being unable to log in due to "account security", but some WeChat accounts do not This kind of problem seems to be the problem that my WeChat cannot log in to the web version. It has nothing to do with python, and there is no good solution for all parties. In desperation, I had to admit that I was unlucky and borrowed the WeChat of other friends to operate. .
(If) After the login is successful, in order to make fun, we first try to send a WeChat message with the code:
#输入ta的备注
users = itchat.search_friends("郑晓玥")
userName = users[0]['UserName']
print(userName)
#打印确认无误后,输入信息
itchat.send(msg="嘎嘎嘎,在吗", toUserName=userName)
我们还可以给自己发消息:
itchat.send(msg="我是猪 ")
Simple operation, but my own test proves that the fun is endless.
Okay, let’s enter the serious statistics link. First of all, in order to have a macro overview, we can try to extract all the information (nickname, signature, geographic location, etc.) of all friends and store it in an Excel file. Operation code as follows:
# Write information.
table.write(0, 5, u’)
table.write(0, 7, u')
table.write(0, 0, u'【昵称】')
table.write(0, 1, u'【备注名】')
table.write(0, 2, u'【省份】')
table.write(0, 3, u'【城市】')
table.write(0, 4, u'【签名】')
# Loop writing.
a = 0
for i in friends:
table.write(a+1, 0, i['NickName'])
table.write(a+1, 1, i['RemarkName'])
table.write(a+1, 2, i['Province'])
table.write(a+1, 3, i['City'])
table.write(a+1, 4, i['Signature'])
if i['RemarkName'] == u'':
table.write(a+1, 1, u'[ ]')
if i['Province'] == u'':
table.write(a+1, 2, u'[ ]')
if i['City'] == u'':
table.write(a+1, 3, u'[ ] ')
if i['Signature'] == u'':
table.write(a+1, 4, u'[ ] ')
a = a+1
# Name:“weixin_20190000.xls”将结果以Excel形式保存
aaa = 'weixin_'+time.strftime("%Y%m%d", time.localtime())+'.xls'
file.save(aaa)
# Send to filehelper.
itchat.send('made by persimmon', 'filehelper')
itchat.send('@%s@%s' % ('fil', aaa), 'filehelper')
print("over")
Then we can find a complete but messy Excel file from our work path, but this does not lead to any useful conclusions. Next, let us divide into different modules to explore the secrets of the circle of friends (funny.jpg).
1. Male to female ratio of WeChat friends
First of all, what we are most interested in and the best thing to do is of course the ratio of the number of men and women.
Get the friend list first:
friends = itchat.get_friends(update=True)[0:]
Then start to calculate the number of males and females in a loop (note that because some people do not indicate their gender, they are divided into three categories here):
#初始化计算器
male = female = others = 0
for i in friends[0:]:
sex = i["Sex"]
if sex == 1:
male += 1
elif sex == 2:
female += 1
else:
others += 1
#以浮点数形式表示数据
total = len(friends[0:])
num_1 = float(male)
num_2 = float(female)
num_3 = float(others)
#打印结果
print(total)
print(num_1)
print(num_2)
print(num_3)
接下来计算男女比例:
total = len(friends[1:]) # 总数,男,女,其他
num1 = float(male) / total * 100
num2 = float(female) / total * 100
num3 = float(others) / total * 100
print(" 的微信好友数:" + str(total)) # 输出
print("男性好友比例:" + str(num1) + "%" + "\n" + "数量:" + str(float(male)))
print("女性好友比例:" + str(num2) + "%" + "\n" + "数量:" + str(float(female)))
print("未填性别:" + str(num3) + "%" + "\n" + "数量" + str(float(others)))
got the answer:
Number of WeChat friends: 264
Proportion of male friends: 27.65151515151515%
Quantity: 73.0
Proportion of female friends: 56.439393939393945%
Quantity: 149.0
Unfilled gender: 15.909090909090908%
Quantity 42.0
(Ahem, this is a girl’s WeChat ヽ(*.>Д<)o゜)
If we want to make the data more visual, then we can use the numpy package to achieve chart creation:
import numpy as np
import matplotlib.pyplot as plt
labels ='male','female','other'#各组标签命名
fraces = [73,149,42]#输入数据
plt.axes(aspect=1)
plt.pie(x=fraces,labels= labels,autopct='%0f%%')#显示各组百分比
plt.pie(x=fraces,labels= labels)
plt.show()
The results are as follows:
2. Friend Personalized Signature Word Cloud
The word cloud is a very magical and interesting thing, now let's try to generate a word cloud with the signatures of our friends! First, repeat the previous login and get the friend list operation, and then:
for i in friends:
signature = i["Signature"]# 获取个性签名
print signature# 打印个性签名
After printing, you will find that there are a large number of fields such as class, emoji, and so on. Because emojis are used in the personalized signature, these fields are to be filtered out:
for i in friends:
signature = i["Signature"].strip().replace("span", "").replace("class", "").replace("emoji", "")
rep = re.compile("1f\d.+")
signature = rep.sub("", signature)
print signature
Next, use jieba to segment words, and then make it into a word cloud, first install jieba and wordcloud library
pip install jieba
pip install wordcloud
import itchat
import re# coding:utf-8
itchat.login()
friends = itchat.get_friends(update=True)[0:]
tList = []
for i in friends:
signature = i["Signature"].replace(" ", "").replace("span", "").replace("class", "").replace("emoji", "")
rep = re.compile("1f\d.+")
signature = rep.sub("", signature)
tList.append(signature)
text = "".join(tList) # 拼接字符串
import jieba
wordlist_jieba = jieba.cut(text, cut_all=True)
wl_space_split = " ".join(wordlist_jieba) # jieba分词
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import PIL.Image as Image# wordcloud词云
my_wordcloud = WordCloud(background_color="white", max_words=2000,
max_font_size=40, random_state=42,
font_path='/Users/sebastian/Library/Fonts/Arial Unicode.ttf').generate(wl_space_split)
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()
3. Draw a geographic distribution map of friends
Want to know where all your friends come from? Then try to draw a map with the obtained friend provinces and cities!
# map-distribution
Province = get_var('Province')
data = pd.DataFrame({'prv':Province, 'nu':1})
data = data.loc[data['prv']!='', :]
data = data.groupby(['prv']).sum().sort_values(by='nu')
province = data.index.tolist()
values = data.values.tolist()
C = Collector()
@C.funcs
def map_base() -> Map:
c = (Map().add("", [list(z) for z in zip(province, values)], "china")
.set_global_opts(title_opts=opts.TitleOpts(title="微信好友地区分布(连续型)"), visualmap_opts=opts.VisualMapOpts(max_=50)
return c
Page().add(*[fn() for fn, _ in C.charts]).render("map-distribution of friends.html")
# analyse signature
# it has some unknown mistake.
siglist = []
for i in friends:
signature = i["Signature"].strip().replace("span", "").replace("class", "").replace("emoji", "")
rep = re.compile("1f\d+\w*|[<>/=]")
signature = rep.sub("", signature)
siglist.append(signature)
text = "".join(siglist)
wordlist = jieba.cut(text, cut_all=True)
word_space_split = ",".join(wordlist).split(',')
m = []
for i in word_space_split:
m.append(i)
da = pd.DataFrame({'ci':m, 'nu':1})
da = da.groupby(['ci']).sum().sort_values(by='nu', ascending=False)
da.to_csv('d:/qianming.csv')
# Preserve images that were used by my friends into local computer
username = get_var('UserName')
for i, uname in enumerate(username):
with open("headImg/"+str(i)+".png", "wb") as f:
img = itchat.get_head_img(uname)
f.write(img)
x = 0
y = 0
imgs = os.listdir("headImgs")
random.shuffle(imgs)
input_lengh = int(input("2160"))
input_width = int(input("1080"))
new_img = Image.new('RGBA', (input_width, input_lengh))
width = int(math.sqrt(input_lengh * input_width / len(imgs)))
num_line = int(input_width/width)
for i in imgs:
try:
img = Image.open("headImage/" + i)
except IOError:
print("第{}张图片为空".format(i))
else:
img = img.resize((width, width), Image.ANTIALIAS)
new_img.paste(img, (x * width, y * width))
x += 1
if x >= num_line:
x = 0
y += 1
new_img.save("mixImg.png")
itchat.send_image('mixedImg.png')
That's it for the analysis of WeChat friends (God Wu Qiyong)! There are also unlimited functions such as automatic reply, friend chat frequency statistics, generation of friends' avatar collections, retrieval of withdrawn messages, finding who deleted themselves... etc. waiting for everyone to use python and itchat libraries to explore freely.