I used Python to crawl my Wechat friends (examples) and python examples

Source: Internet
Author: User

I used Python to crawl my friends (for example ).

I have never worked hard in recent days. I accidentally learned about the itchat package in Python last night. It has completed the personal account API interface of wechat, making it easier to crawl personal information. Since I had long wanted to know which city my friend's gender ratio came from, I decided to climb my own.

Author: Alfred

First, install the itchat package on the terminal.

Import the package after installation, and then log on to your own package. A login QR code will be generated during the process. You can log on after scanning the QR code. After logging in, you can crawl the information of your friends.

With the above friends data, we can analyze it.

Proportion of men and women with friends

After carefully observing the returned data structure, we found that gender is stored in a dictionary, key is Sex, male is 1, and female is 2, others are of unknown gender (that is, they are not filled in ). You can write a loop to get the desired gender data and get the gender ratio of your friends.

The output is as follows:

Male friend: 37.65%

Female friends: 59.23%

Friend of unknown gender: 3.12%

Ah, accidentally exposed the fact that many female friends exist. But why am I still a wang ?! Well, let's use the R to draw a graph. (The Python plot is really unbearable and the code will not be put ):

City distribution of friends

After carefully observing the friends list, we found that it also contains friends nickname, province, city, personal profile, and other data, which can be used to analyze the distribution of friends cities, the best way is to define a function that crawls data and stores it in a data box for analysis.

The preceding figure shows a csv desktop file named data. You can use R to open the file and perform data preprocessing. The following figure is displayed (privacy-related files are pre-processed ):

Next, we first group and aggregate data based on provinces and cities, select the top 20 cities, and use the ggplot2 package to draw the following urban distribution map (the code is too long, so capricious, for more information, see ):

It seems that most of my friends are in Guangdong (not nonsense). Most of my friends in Guangdong are in Guangzhou, Shenzhen, Pearl, and the second is in Auckland, followed by Sichuan and Macao. The gray NA value refers to a friend who has not set his own location. There are more than 70 people in total.

In addition, many foreign cities are mistakenly regarded as provinces due to setting problems (many of them skip provinces directly and only cities can choose.

Let's look at the specific distribution of our friends in Guangdong (take the first eight ):

Custom word cloud for personal signatures of your friends

Fun. I have previously crawled the personalized signatures of every friend, so I can analyze the frequently used words for personalized signatures. By the way, I can create a word cloud.

Print the previously crawled Signature and find many irrelevant words, such as emoji, span, and class, replace the string first. In addition, there are symbols such as <>/=. You also need to write a simple regular expression to replace the Regular Expression and then combine all the strings to obtain the text string.
Then we can break JB, no, into the package of jieba word segmentation.

You can finally enter the drawing stage. You can draw similar images based on your desired images, shapes, and colors (here, I use my avatar, of course, in order to make the final word cloud look better and easier to distinguish, I made a little bit of processing for my profile picture ). For this reason, we need to include matplotlib, wordcloud, numpy, PIL, and other packages.

Run the above Code to obtain the following figure:

It seems that in my friend's personal signature, some people are good at listening, some emphasize kindness, some pay attention to the soul, and others stare at life. Keep waiting and don't give up searching. Twists and turns, do not fear hypocrisy. If you are sincere, you cannot live up to it. One step, one more effort, one more than one dream ).

Last

Is the above interesting? Did it open the door to a new world?

My friends are not too many. If there are thousands of friends, you can get thousands of data records, which is very valuable for analysis.

Of course, the itchat package also has many other features to be explored, including automatic reply to information, automatic addition of friends, and management group. If you have time, try again.

This article uses Python to crawl all the content shared by my friends (examples). I hope to give you a reference and support the house of helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.