Python實現(xiàn)的微信好友數(shù)據(jù)分析功能示例
本文實例講述了Python實現(xiàn)的微信好友數(shù)據(jù)分析功能。分享給大家供大家參考,具體如下:
這里主要利用python對個人微信好友進行分析并把結(jié)果輸出到一個html文檔當中,主要用到的python包為itchat,pandas,pyecharts等
1、安裝itchat 微信的python sdk,用來獲取個人好友關系。獲取的代碼 如下:
import itchat
import pandas as pd
from pyecharts import Geo, Bar
itchat.login()
friends = itchat.get_friends(update=True)[0:]
def User2dict(User):
User_dict = {}
User_dict["NickName"] = User["NickName"] if User["NickName"] else "NaN"
User_dict["City"] = User["City"] if User["City"] else "NaN"
User_dict["Sex"] = User["Sex"] if User["Sex"] else 0
User_dict["Signature"] = User["Signature"] if User["Signature"] else "NaN"
User_dict["Province"] = User["Province"] if User["Province"] else "NaN"
return User_dict
friends_list = [User2dict(i) for i in friends]
data = pd.DataFrame(friends_list)
data.to_csv('wechat_data.csv', index=True)
2、對獲取到的數(shù)據(jù)進行分析。
主要分析了男女比例,以及好友所在城市分布,并且在地圖上面展示了微信好友的分布情況。另外其他的數(shù)據(jù)讀者可以自己去分析,這里只是提供一個引導而已。
import pandas as pd
from pyecharts import Geo, Bar
def Cal_mVw(data):
result = {}
for i in data:
if i == 1:
result["man"] = result.get("man", 0) + 1
elif i == 2:
result["woman"] = result.get("woman", 0) + 1
else:
result["unknown"] = result.get("nunknown", 0) + 1
return result
def count_city(data):
result = {}
for i in data:
if data is not "NaN" or data is not "nan":
result[i] = result.get(i, 0) + 1
return result
data1 = pd.read_csv('wechat_data.csv', encoding='GBK')
manVSwoman=Cal_mVw(data1["Sex"])
#print(manVSwoman)
bar = Bar("個人微信好友男女比例")
bar.add("男女人數(shù)", ["男", "女", "不詳"], [139, 75, 1])
bar.render()
city=count_city(data1["City"])
geo = Geo("微信好友分布", "", title_color="#fff", title_pos="center",
width=1200, height=600, background_color='#404a59')
#attr, value = geo.cast(city)
geo.add("", city.keys(), city.values(), visual_range=[0, 30], visual_text_color="#fff", symbol_size=15, is_visualmap=True)
geo.show_config()
geo.render()
男女比例畫出來的圖如下所示

獲取到的好友分布情況如下圖所示:

更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
相關文章
Anaconda和ipython環(huán)境適配的實現(xiàn)
這篇文章主要介紹了Anaconda和ipython環(huán)境適配的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04
Python?第三方opencv庫實現(xiàn)圖像分割處理
這篇文章主要介紹了Python?第三方opencv庫實現(xiàn)圖像分割處理,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06

