基于python實現(xiàn)微信好友數(shù)據(jù)分析(簡單)
一、功能介紹
本文主要介紹利用網(wǎng)頁端微信獲取數(shù)據(jù),實現(xiàn)個人微信好友數(shù)據(jù)的獲取,并進(jìn)行一些簡單的數(shù)據(jù)分析,功能包括:
1.爬取好友列表,顯示好友昵稱、性別和地域和簽名, 文件保存為 xlsx 格式
2.統(tǒng)計好友的地域分布,并且做成詞云和可視化展示在地圖上
二、依賴庫
1、Pyecharts:一個用于生成echarts圖表的類庫,echarts是百度開源的一個數(shù)據(jù)可視化庫,用echarts生成的圖可視化效果非常棒,使用pyechart庫可以在python中生成echarts數(shù)據(jù)圖。
2、Itchat:一個開源的微信個人號接口,使用python調(diào)用微信從未如此簡單。
3、Jieba:簡單的分詞操作庫。
4、Numpy:NumPy 系統(tǒng)是 Python 的一種開源的數(shù)值計算擴(kuò)展。這種工具可用來存儲和處理大型矩 陣。
5、Pandas:pandas 是基于 NumPy 的一種工具,該工具是為了解決數(shù)據(jù)分析任務(wù)而創(chuàng)建的。
6、Pillow:圖像處理。
7、wxpy:wxpy 在 itchat 的基礎(chǔ)上,通過大量接口優(yōu)化提升了模塊的易用性,并進(jìn)行豐富的功能 擴(kuò)展。 (微信本身提供)
注:Pyecharts可能安裝0.5.*的版本比較好
以上的三方庫可以通過命令符(cmd)來實現(xiàn)安裝,具體命令:pip install ***
三,操作
from wxpy import * #導(dǎo)入模塊 bot = Bot(cache_path=True) #初始化機(jī)器人,選擇掃碼登錄 friend_all = bot.friends() #獲取微信好友信息
首先出現(xiàn)的是一張二維碼,然后掃描登錄

成功登錄好了就是這種顯示

之后就可以進(jìn)行操作了,好友數(shù)量,個人信息
print(len(friend_all)) #好友的數(shù)量 print(friend_all[0].raw) #輸出個人信息
顯示的結(jié)果

四、接下來把全部的好友信息轉(zhuǎn)化為一個xlsx文件
獲取全部好友信息
for a_friend in friend_all:
NickName = a_friend.raw.get('NickName', None)
#昵稱
#Sex = a_friend.raw.get('Sex', None)
Sex = {1: "男", 2: "女", 0: "其它"}.get(a_friend.raw.get('Sex', None), None)
#性別(優(yōu)化)
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)
#小Flag
list_0=[NickName, Sex, City, Province, Signature, HeadImgUrl, HeadImgFlag]
#存為一維數(shù)組
lis.append(list_0)
#疊加數(shù)據(jù)
存為xlsx文件
def list_excel(filename,lis):
'''
將列表寫入excel中,其中列表中的元素是列表.
filename:保存的文件名(含路徑)
lis:元素為列表的列表,如下:
lis = [["名稱", "價格", "出版社", "語言"],
["暗時間", "32.4", "人民郵電出版社", "中文"],
["拆掉思維里的墻", "26.7", "機(jī)械工業(yè)出版社", "中文"]]
'''
import openpyxl
wb = openpyxl.Workbook() #激活worksheet
sheet = wb.active
sheet.title = 'sheet1' #創(chuàng)建一個表格
file_name = filename +'.xlsx'
for i in range(0, len(lis)):
for j in range(0, len(lis[i])):
sheet.cell(row=i+1, column=j+1, value=str(lis[i][j]))
#每行每列的存入數(shù)據(jù)
wb.save(file_name)
print("寫入數(shù)據(jù)成功!")
list_excel('wechat',lis)
效果如下:

可以看到其好友基本分布再廣東省,個性簽名也是非常的殺馬特
五、實現(xiàn)詞云圖(我們也可以從存儲在本地的 excel 中讀取數(shù)據(jù)進(jìn)行分析,并查看數(shù)據(jù)形式。在執(zhí)行以 下代碼之前,我們需要先把 excel 文件加一個列標(biāo)題行)
例如nickname sex city province signature headImgUrl headImgFlag
#導(dǎo)入模塊
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import pandas as pd
from pandas import DataFrame
word_list= df['city'].fillna('0').tolist()
#將 dataframe 的列轉(zhuǎn)化為 list,其中的 nan 用“0”替換
new_text = ' '.join(word_list)
wordcloud = WordCloud(font_path='simhei.ttf', background_color="black").generate(new_text)
#設(shè)計圖背景顏色,字體
plt.imshow(wordcloud)
plt.axis("off")
plt.show()

還可以將詞云圖存為HTML形式
#利用 pyechart 做詞云
import pandas as pd
#count = df.city.value_counts() #對 dataframe 進(jìn)行全頻率統(tǒng)計,排除了 nan
city_list = df['city'].fillna('NAN').tolist()#將 dataframe 的列轉(zhuǎn)化為 list,其中的 nan 用“NAN” 替換
count_city = pd.value_counts(city_list)#對 list 進(jìn)行全頻率統(tǒng)計
from pyecharts.charts.wordcloud import WordCloud #設(shè)置對象
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:\python\wechatcloud.html')
再看看效果:

六、轉(zhuǎn)化為地圖形式
注:安裝地圖數(shù)據(jù)包:pip install echarts-china-provinces-pypkg pip install echarts-countries-pypkg
province_list = df['province'].fillna('NAN').tolist()
#將 dataframe 的列轉(zhuǎn)化為 list,其中的 nan 用 “NAN”替換
count_province = pd.value_counts(province_list)
#對 list 進(jìn)行全頻率統(tǒng)計
from pyecharts import Map
value =count_province.tolist()
attr =count_province.index.tolist()
map=Map("各省微信好友分布", width=1300, height=700)
map.add("", attr, value, maptype='china', is_visualmap=True,visual_text_color='#000',is_label_show = True)
#顯示地圖上的省份
map.show_config()
map.render(r'D:\python\wechatProMap.html')
效果:

總結(jié)
以上所述是小編給大家介紹的基于python實現(xiàn)微信好友數(shù)據(jù)分析,希望對大家有所幫助!
相關(guān)文章
Pandas數(shù)據(jù)分組統(tǒng)計的實現(xiàn)示例
對數(shù)據(jù)進(jìn)行分組統(tǒng)計,主要適用DataFrame對象的groupby()函數(shù),本文就來詳細(xì)的介紹下Pandas數(shù)據(jù)分組統(tǒng)計的實現(xiàn),具有一定的參考價值,感興趣的可以了解下2023-11-11
TensorFlow深度學(xué)習(xí)之實現(xiàn)合并與分割的示例代碼
這篇文章主要為大家詳細(xì)介紹了TensorFlow中實現(xiàn)合并與分割的四位函數(shù)以及它們的用法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-07-07
Python的Flask框架中@app.route的用法教程
這篇文章主要介紹了Python的Flask框架中@app.route的用法教程,包括相關(guān)的正則表達(dá)式講解,是Flask學(xué)習(xí)過程當(dāng)中的基礎(chǔ)知識,需要的朋友可以參考下2015-03-03

