Our friends are from all corners of the country. We can use python's powerful database to realize the statistics of friends.
Tools: wxpy library, wordcloud library, matplotlib library, openpyxl library, pandas library, numpy library.
1, first realize the landing and acquisition of friend data operation.
Code:
//If you are interested in python, I have a Python learning base, which has a lot of learning materials, interested.+Q Group:688244617 #Introducing WeChat landing interface from wxpy import * #Obtaining logon two-dimensional code bot = Bot(cache_path = True) #Get basic data from WeChat friends friend_all = bot.friends() #Create a two-dimensional list to store basic friend information lis = [['NickName','Sex','City','Province','Signature','HeadImgUrl','HeadImgFlag']] #Save the feature data as a list for a_friend in friend_all: NickName = a_friend.raw.get('NickName', None) Sex = {1:"male", 2:"female", 0:"Other"}.get(a_friend.raw.get('Sex', None), None) City = a_friend.raw.get('City', None) Province = a_friend.raw.get('Province', None) Signature = a_friend.raw.get('Signature', None) HeadImgUrl = a_friend.raw.get('HeadImgUrl', None) HeadImgFlag = a_friend.raw.get('HeadImgFlag', None) list_0 = [NickName, Sex, City, Province, Signature, HeadImgUrl, HeadImgFlag] lis.append(list_0)
Operation results:
At this point, a two-dimensional code is popped up, and the data of friends can be acquired automatically by scanning the code and landing.
2. Save Friend Data as xlsx Files
Convenient processing of world cloud Libraries
Code:
#Convert lists to xlsx tables def list_to_xlsx(filename, list): import openpyxl wb = openpyxl.Workbook() sheet = wb.active sheet.title = 'Friends' file_name = filename + '.xlsx' for i in range(0, len(list)): for j in range(0, len(list[i])): sheet.cell(row = i+1, column = j+1, value = str(list[i][j])) wb.save(file_name) print("Read and write data successfully") #Generate lists into tables list_to_xlsx('wechat_friend', lis)
Operation results:
Generated an xlsx file
3. Generative Word Cloud
Generating a word cloud using the world cloud Library
Code:
from wordcloud import WordCloud import matplotlib.pyplot as plt import pandas as pd from pandas import read_excel import numpy as np df = read_excel('wechat_friend.xlsx') #Generating word clouds using WordCloud word_list = df['City'].fillna('0').tolist() new_text = ' '.join(word_list) wordcloud = WordCloud(font_path='msyh.ttc', background_color = 'white').generate(new_text) plt.imshow(wordcloud) plt.axis("off") plt.show() #Using pyecharts to generate word clouds from pyecharts import WordCloud city_list = df['City'].fillna('').tolist() count_city = pd.value_counts(city_list) name = count_city.index.tolist() value = count_city.tolist() wordcloud = WordCloud(width=1300,height=620) wordcloud.add("",name,value,word_size_range=[20,100]) #wordcloud.show_config() #wordcloud.render(r'D:\wc.html') wordcloud.render('wordcloud.html') #Show your friends on the map from pyecharts import Map province
Operation results:
Two. Create a robot.
Tools: requests module, itchat module
Then we apply for api interface http://www.itpk.cn on Molly Robot.
Code:
#-*- coding:utf-8 -*- import itchat import requests def get_response(msg): apiurl = 'http://i.itpk.cn/api.php' //moli Robot's Web Site data={ "question": msg, //Get Text Information for Chat "api_key": "9ddf52cacd0ef429d1c63bf411b9bed6", "api_secret": "n4gxkdyckd7p" } r=requests.post(apiurl,data=data) //Constructing network requests return r.text @itchat.msg_register(itchat.content.TEXT) //Dealing with Friends'News def print_content(msg): return get_response(msg['Text']) @itchat.msg_register([itchat.content.TEXT], isGroupChat=True) //Group Message Processing def print_content(msg): return get_response(msg['Text']) itchat.auto_login(True) //automatic logon itchat.run() //Start a chat robot
Just run the login.