第一个微信项目

一·统计微信好友信息

需用wxpy库,再用手机扫描二维码登陆。

#导入模块 
from wxpy import * 
 
#初始化机器人,选择缓存模式(扫码)登录 
bot = Bot(cache_path=True) 
 
#获取我的所有微信好友信息 
friend_all = bot.friends()

接下来获取好友信息:

lis=[]
for a_friend in friend_all:
    NickName = a_friend.raw.get('NickName',None)
    Sex ={1:"",2:"",0:"其它"}.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)
    list_0=[NickName,Sex,City,Province,Signature]
    lis.append(list_0)

将信息储存在EXCEL表格中:

def toex(lis):
    text=pd.DataFrame(lis,columns=['微信名','性别','城市','省份','个性签名'])
    text.to_excel('wx.xlsx',encoding='U0001f31a')
    print(1)

效果如下:

二·用jieba库做词云

#jieba库精确模式分词
wordlist = jieba.lcut(cityStr)
cityStr = ' '.join(wordlist)
    # 加载背景图片
    #cloud_mask = np.array(Image.open(BackGroundFile))
    #设置词云图属性
font = r'C:WindowsFontssimfang.ttf' # 设置字体路径
wc = WordCloud(
    background_color = 'black',     # 背景颜色
    #mask = cloud_mask,             # 背景图片
    max_words = 100,                # 设置最大显示的词云数
    font_path = font,               # 设置字体形式(在本机系统中)
    height = 300,                   # 图片高度
    width = 600,                    # 图片宽度
    max_font_size = 100,            # 字体最大值
    random_state = 100,             # 配色方案的种类
    )
    # 生成词云图
myword = wc.generate(cityStr)
    #展示词云图
plt.imshow(myword)
plt.axis('off')
plt.show()

效果如下:

三·用pyecharts库做微信好友省份

import pandas as pd
from pyecharts import Map 
df=pd.read_excel('wx.xlsx')
pr_list = df['省份'].fillna('pr').tolist()
count_pr = pd.value_counts(pr_list)
attr =count_pr.index.tolist() 
value = count_pr.tolist()
maap=Map("各省微信好友分布", width=1200, height=600)
maap.add("", attr, value, maptype='china', is_visualmap=True,visual_text_color='#000', is_label_show = True)
maap.show_config()
maap.render(r'wxpr.html')
print(1)

效果如下:

微信好友广东省内分布:

原文地址:https://www.cnblogs.com/zk1135/p/10977161.html