zoukankan      html  css  js  c++  java
  • 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()

  • 相关阅读:
    vue-cli构建项目 npm run build后应该怎么运行在本地查看效果
    解析JSON数组
    Lambda
    keytool不是内部或外部命令
    XML布局
    HTML5本地存储IndexedDB基础使用
    Vue 2.0基础
    cs231n__3. LostFunction
    cs231n__2. K-nearest Neighbors
    week_Last
  • 原文地址:https://www.cnblogs.com/swjian/p/10597690.html
Copyright © 2011-2022 走看看