首页 / PYTHON / 使用python进行微信好友分析
使用python进行微信好友分析
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用python进行微信好友分析,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2889字,纯文字阅读大概需要5分钟。
内容图文
![使用python进行微信好友分析](/upload/InfoBanner/zyjiaocheng/813/dfe7329a4a154dfd9756795ceead369f.jpg)
1. 使用到的库
① wxpy:初始化微信机器人
② openpyxl:保存微信好友数据为Excel表格
③ pyecharts:生成可视化的地图
④ wordcloud、matplotlib、jieba:生成词云图
2. 基本功能
① 分析微信好友数据
② 生成词云图
③ 生成地图展示
3.初始化机器人和获取微信好友的源信息
from wxpy import*
bot=Bot(cache_path=True)
friend_all=bot.friends()
len(friend_all)
Friends=bot.friends()
data=Friends.stats_text(total=True,sex=True,top_provinces=30,top_cities=500)
print(data)
from wxpy import * #获取登录二维码
bot = Bot(cache_path = True) #获取微信朋友的基本数据
friend_all = bot.friends() #建立一个二维列表,存储基本好友信息
lis = [['NickName','Sex','City','Province','Signature','HeadImgUrl','HeadImgFlag']]
#把好有特征数据保存为列表
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)
??? 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) #把列表转换为 xlsx 表格
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("读写数据成功") #把列表生成表格
list_to_xlsx('wechat_friend', lis)
???
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') #使用 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()
#使用 pyecharts 生成词云
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')
#将好友展示在地图上
from pyecharts import Map province _list = df['Province'].fillna('').tolist()
count_province = pd.value_counts(province_list)
attr = count_province.index.tolist()
value1 = count_province.tolist()
map = Map("各省微信好友分布", width=1200,height=600)
map.add("",attr, value1, maptype='china',is_visualmap=True,visualmap_text_color='#000',is_label_show=True)
#map.show_config()
map.render('map.html') ?
![使用python进行微信好友分析 - 文章图片](/upload/getfiles/0001/2021/5/5/20210505034720545.jpg)
![使用python进行微信好友分析 - 文章图片](/upload/getfiles/0001/2021/5/5/20210505034721929.jpg)
内容总结
以上是互联网集市为您收集整理的使用python进行微信好友分析全部内容,希望文章能够帮你解决使用python进行微信好友分析所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。