Everybody is good!

Because there is nothing to do, I want to look at my wechat friends, and then rationally analyze a wave of ~~

01 Data Collection

We use Itchat library to obtain wechat friend data this time.

01 landing

To use Itchat library to obtain wechat friend data, you need to log in first. The code is as follows:

itchat.auto_login(hotReload=True)
Copy the code

Where hotReload=True is used to obtain data in a short period of time without repeated login verification.

02 Obtaining friend data

The get_friends() function of the Itchat library retrieves data for all friends. But the data type it gets is an Itchat type. Since we don’t need to use the re to extract the data, we need to convert the data to a string type, as follows:

all_friends = str(itchat.get_friends())
Copy the code

At this point, we can start to extract the data, here we extract friends’ personality signature, friends’ gender, friends’ province and city, these four data for visual display. The code is as follows:

# Signature Signature = re. The.findall (" 'Signature' : '([\ u4e00 - \ u9fa5]. *?) ',",all_friends) c = 0 for I in Signature: with open(r' signature.txt ','a') as f: try: f.write(I) except: Sex = re.findall("'Sex': (.*?)) ,",all_friends) man = woman = other = 0 for i in Sex: if i == '1': man+=1 elif i == '2': woman+=1 else: Shengfens = re.findall(r"' type ': '(.*?) ',",all_friends) chengshis = re.findall(r"'City': '(.*?) Shengfen = [] for I in range(len(shengfens)): if shengfens[I] == ": pass else: Shengfens [I] chengshi = [] for I in range(len(chengshis)): if shengfens[I] == '1 ': chengshi.append(chengshis[i])Copy the code

02 Visual Display

We have obtained a total of 973 friends’ data. The following is a visualization of these data.

01 Visualization of signature cloud

Through the word cloud visualization of all friends’ personality signatures, we found that “effort”, “life”, “time”, “world” and “no” were the most common words. It seems that my friends tend to prefer things related to these words.

The code is as follows:

With open(" sign.txt ",) as f: job_title_1 = f.read() job_title_2 = re.sub('span','',job_title_1) job_title_3 = re.sub('class','',job_title_2) Job_title_4 = re. Sub (' emoji ', ' ', job_title_3) job_title_5 = re. The sub (' yourself ' ' ', job_title_4) job_title_6 = Re.sub (' reply ', ",job_title_5) # job_title_7 = re.sub(' at ', ",job_title_6) Contents_cut_job_title = jieba.cut(job_title_6) Contents_list_job_title = "". Join (contents_cut_job_title) wc = WordCloud(stopwords= stopwords.add (" one "), Collocations =False, background_color="white", font_path=r" TTF ", width=400, height=300, random_state=42, Mask =imread(' xin-.jpg ', pilmode="RGB") wc.generate(contents_list_job_title) wC.to_file ("Copy the code

02 Sex quantity chart

By visualizing the gender of our friends, we found that we had 543 male friends, 318 female friends, and 112 friends who didn’t fill out this information.

03

Province map

By visualizing the provinces where the 973 friends live, we found that Henan has the largest number of friends with 263, followed by Guangdong with 69. Henan has the most friends, probably because Zhibin is from Henan.

The code is as follows:

province_distribution = dict(Counter(shengfen).most_common()) provice = list(province_distribution.keys()) values = List (province_distribution. Values ()) map = map ("中国地 址 ",width=1200, height=600) map. Add ("", provice, values, visual_range=[0, 200], maptype='china', is_visualmap=True, Visual_text_color ='#000',is_label_show=True) map.render(path=" map.html ")Copy the code

04 Distribution of friends in Henan Province

Through the above analysis, we found that Henan had the most friends, so I then visualized the city distribution of my friends in Henan province.

From the diagram, we find that zhengzhou has the largest number of friends, with 116, followed by Hebi, with 38. I live in Hebi, and I study in Zhengzhou. Zhengzhou has more friends than Hebi, which is probably caused by mobile phones in university.

The code is as follows:

city = [] values = [] for k,v in dict(Counter(chengshi).most_common()).items(): Append (k+' x ') values. Append (v) map2 = Map(' x ', width=1200, height=600) map2. Add (' x ', city, values, Visual_range =[1, 25], is_VisualMap =True, visual_text_color='#000')Copy the code