How to Scrape and Visualize Your WeChat Friends’ Locations with Python
Learn step-by-step how to use Python’s itchat library to extract your WeChat contacts’ province and city information, handle QR code authentication, parse the data, and create visual maps of friend distribution, all illustrated with screenshots and code snippets.
Previously I shared a tutorial on using Python web scraping to retrieve the number of WeChat friends and their gender ratio; you can view that article via the provided link. This article explains how to scrape the province and city information of WeChat friends and visualize the results.
The itchat library is essential for accessing WeChat friend data. Below is the code for obtaining friends' province information:
After running the program, you need to scan the QR code for authentication. The console will display red messages indicating QR code scanning, authorization, loading contacts, and successful login. The extracted data is then printed, as shown in the following screenshots.
The printed content can be saved to a text file for analysis. The data reveals that most friends are located in China, with Liaoning, Guangdong, and Hunan provinces having the highest counts. Other provinces such as Qinghai appear only abroad, and there are no friends from Yunnan, Hainan, or Gansu.
A map visualization of the friend distribution highlights Liaoning, Guangdong, and Hunan as the regions with the most contacts.
Next, the code for extracting friends' city information is presented:
The output shows that Dalian, Guangzhou, Shenzhen, and cities in Hunan such as Hengyang and Changsha are the most common among friends. The city distribution can also be saved to a text file for clearer inspection.
Additional city data is shown in the following images, and readers are encouraged to aggregate the city statistics and create their own map visualizations.
Enjoy experimenting with the visualizations and have a great weekend!
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
