I used Python to crawl WeChat friends, and finally found a big secret

foreword

What kind of environment you live in, what kind of person you will become. Nowadays, people's daily life is basically inseparable from WeChat, but WeChat is not just an instant messaging software, WeChat is more like a virtual real world. What kind of circle of friends you are in will gradually change your thinking. I was learning itchat recently, and then I wrote a crawler that crawls the data of all my WeChat friends. I analyzed some of the data and found some interesting things.

Then through the itchat.get_friends() function, you can get the relevant information of your friends, which is returned as a json data. Then we can perform regular matching based on the returned information to grab the information we want and analyze it.

import itchat

itchat.login()

# Crawl information about your friends and return a json file

friends = itchat.get_friends(update=True)[0:]

Male to female ratio of my WeChat friends

Observing the returned data, it is easy to find that the gender is stored in a dictionary based on the keyword. Its key is "Sex", the male value is 1, the female value is 2, and the others are of unknown gender (that is, not filled in).

In the code, I define a function parse_friends() through a for loop, and save the obtained data to the text dictionary through the for loop. Then draw the histogram through the draw() function.

The histogram uses the plt library. I also wrote an article "Python produces English learning materials worth tens of thousands" before. Interested students can check it out.

The final printed result:

I have to say that my WeChat friends with more than 1K friends have a very discordant gender ratio, with more men than women. This reminds me of a high school class with 50 students and only 7 girls, and the girls in our class have a girl group called "Seven Fairies" since then.

Word cloud of personal signatures of my WeChat friends

In order to further analyze the general characteristics of my friends, I grabbed the personal signatures of my friends and analyzed them into a word cloud.

Personalized signatures are stored in the Signature key. Since some signatures contain expressions, the initial capture will turn into unrelated words such as emoji, span, class, and so on. So you need to replace it first. In addition, there are symbols like /=, you also need to write a simple regular replacement, and then spell everything together to get the text string.

The obtained data is finally saved to the text in the current directory named "text.txt".

I define the function to analyze the friend's signature as: parse_signature(). The complete code is as follows:

After grabbing and organizing the signed data, the next step is to create a word cloud. The wordCloud is used here to make the word cloud. The previous article also introduced the production of word clouds. Interested students can check this article "Kobe in My Eyes, Forever Superstar".

For the production of word cloud, I defined a :draw_signature() function, the complete code is as follows

After running the above code, I got the following picture. Due to the large number of friends, I found two pictures to make a picture cloud.

Work hard, struggle, the world, life, yourself. These words appear the most among our 1K+ friends. Everyone is very good, and they are all very motivated. I think this is why I study hard every day, the environment really affects a person.

The complete code I have uploaded to GitHub:

https://github.com/chihaiyishen/Python-Learning/tree/master/python%20%E7%88%AC%E8%99%AB/wechat

Author: crazy sea

Reprint link: https://news.html5.qq.com/share/9072422611354729272?url=http%3A%2F%2Fkuaibao.qq.com%2Fs%2F20180503G07JCV00&ch=060000&qbredirect=&share=true&sc_id=ICoTfhC

 

Public number: 51reboot operation and maintenance development

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325247571&siteId=291194637