Python 實現獲取微信好友信息


  最近用閑余時間看了點python,在網上沖浪時發現有不少獲取微信好友信息的博客,對此比較感興趣,於是自己敲了敲順便記錄下來。

一、使用 wxpy 模塊庫獲取好友男比例信息和城市分布。

# -*- coding: utf-8 -*-
"""
微信好友性別及位置信息
"""

#導入模塊
from wxpy import Bot

'''Q
微信機器人登錄有3種模式,
(1)極簡模式:robot = Bot()
(2)終端模式:robot = Bot(console_qr=True)
(3)緩存模式(可保持登錄狀態):robot = Bot(cache_path=True)
'''
#初始化機器人,選擇緩存模式(掃碼)登錄
robot = Bot(cache_path=True)

#獲取好友信息
robot.chats()
#robot.mps()#獲取微信公眾號信息

#獲取好友的統計信息
Friends = robot.friends()
print(Friends.stats_text())

得到的好友信息結果

二、使用 itchat 獲取好友詳細信息並輸出到記事本。

import itchat
itchat.login()
friends = itchat.get_friends(update=True)[0:]
def get_var(var):
    variable = []
    for i in friends:
        value = i[var]
        variable.append(value)
    return variable

NickName = get_var("NickName")
Sex = get_var("Sex")
Province = get_var("Province")
City = get_var("City")
Signature = get_var("Signature")
from pandas import DataFrame
data = {'NickName' : NickName,'Sex' : Sex,'Province' : Province,'City' : City,'Signature' : Signature}
frame = DataFrame(data)
frame.to_csv('myFriendanAlyze.txt',index=True)

這個就不展示了,大家想試的話掃描二維碼登陸就可以看到自己好友的信息啦。

三、僅輸出好友占比。

import itchat
itchat.login()
friends = itchat.get_friends(update=True)[0:]

male = female = other = 0

for i in friends[1:]:
    sex = i["Sex"]
    if sex == 1:
        male += 1
    elif sex == 2:
        female += 1
    else:
        other += 1
total = len(friends[1:])

print("男性好友:" + str(male) + ",女性好友:" + str(female) + ",不明好友:"+str(other))

四、圖表展示好友性別分布。

# -*- coding: utf-8 -*-
"""
Created at 2019-3-25 22:50:49
"""

import itchat
import matplotlib.pyplot as plt
from collections import Counter
itchat.auto_login(hotReload=True)
friends = itchat.get_friends(update=True)
sexs = list(map(lambda x: x['Sex'], friends[1:]))
counts = list(map(lambda x: x[1], Counter(sexs).items()))
labels = ['Male','FeMale',   'Unknown']
colors = ['red', 'yellowgreen', 'lightskyblue']
plt.figure(figsize=(8, 5), dpi=80)
plt.axes(aspect=1)
plt.pie(counts,  # 性別統計結果
        labels=labels,  # 性別展示標簽
        colors=colors,  # 餅圖區域配色
        labeldistance=1.1,  # 標簽距離圓點距離
        autopct='%3.1f%%',  # 餅圖區域文本格式
        shadow=False,  # 餅圖是否顯示陰影
        startangle=90,  # 餅圖起始角度
        pctdistance=0.6  # 餅圖區域文本距離圓點距離
)
plt.legend(loc='upper right',)
plt.title('%s的微信好友性別組成' % friends[0]['NickName'])
plt.show()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM