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))

猜你喜欢

转载自www.cnblogs.com/swjian/p/10597690.html