美文网首页Python相关
微信好友初步分析

微信好友初步分析

作者: Linking123 | 来源:发表于2017-08-27 22:11 被阅读0次

    目录

    • 1.微信好友性别比例分析
    • 2.好友个性签名词云

    一个好玩的例子,分析自己的微信好友。做了两个,一个是好友性别比例分析,另一个是好友的个性签名做一个个性化的词云。

    这两个实验在好友量较多时具有一定的价值,我的好友姑且做一个测试吧。

    1.微信好友性别比例分析

    import itchat
    
    # 登录微信,需要扫描弹出的二维码
    itchat.login()
    # 爬取好友信息,json字符串
    friends = itchat.get_friends(update=True)[0:]
    # print(friends) # 测试是否正常输出好友
    
    # 定义一个函数,用来爬取各个变量, return list arr
    def get_var(var):
        variable = []
        for i in friends:
            value = i[var]
            variable.append(value)
        return variable
    
    Sex = get_var('Sex')
    # 初始化男女性别计数器
    male = female = other = 0
    for sex in Sex:
        if sex == 1:
            male += 1
        elif sex == 2:
            female += 1
        else:
            other += 1
    
    # 总数
    total = len(friends)
    
    maleRate = float(male)/total*100
    femaleRate = float(female)/total*100
    otherRate = float(other)/total*100
    
    print('男性好友%d人,占比:%.2f%%' % (male, maleRate))
    print('女性好友%d人,占比:%.2f%%' % (female, femaleRate))
    print('其他性别好友%d人,占比:%.2f%%' % (other, otherRate))
    
    # output
    # 男性好友199人,占比:61.04%
    # 女性好友112人,占比:34.36%
    # 其他性别好友15人,占比:4.60%
    

    我的好友这么少啊,男性还是占多数的。部分好友未设置性别属性。

    绘制比例图

    可视化展示用到了matplotlib.pyplot,看到有人用百度的echarts-python,这个库还不成熟,我用的时候遇到中文编码错误,所以弃了。改用更官方更强大的matplotlib

    # 百分比圆饼表
    import matplotlib.pyplot as plt
    
    # Pie chart, where the slices will be ordered and plotted counter-clockwise:
    labels = 'male', 'female', 'other'
    sizes = [maleRate, femaleRate, otherRate]
    explode = (0, 0.1, 0)  # 突出显示女性比例,嘿嘿 only "explode" the 2nd slice (i.e. 'female')
    
    fig1, ax1 = plt.subplots()
    
    ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.2f%%',
            shadow=True, startangle=90)
    ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
    
    plt.show()
    

    2.好友个性签名词云

    # coding: utf-8
    import itchat
    
    # 登录微信,需要扫描弹出的二维码
    itchat.login()
    # 爬取好友信息,json字符串
    friends = itchat.get_friends(update=True)[0:]
    # print(friends)
    
    #定义一个函数,用来爬取各个变量
    def get_var(var):
        variable = []
        for i in friends:
            value = i[var]
            variable.append(value)
        return variable
    
    # 调用函数得到各变量
    # 个性签名
    Signature = get_var('Signature')
    
    # 很多本来是表情的,变成了 emoji、span、class 等等这些无关紧要的词,需要先替换掉,另外,
    # 还有类似<>/= 之类的符号,也需要写个简单的正则替换掉,再把所有拼起来,得到 text 字串。
    import re
    siglist = []
    for i in friends:
        signature = i["Signature"].strip().replace("span", "").replace("class", "").replace("emoji", "")
        rep = re.compile("1f\d+\w*|[<>/=]")
        signature = rep.sub("", signature)
        siglist.append(signature)
    text = "".join(siglist)
    
    # 结巴分词
    import jieba
    wordlist = jieba.cut(text, cut_all=True)
    word_space_split = " ".join(wordlist)
    
    # 根据自己想要的图片、形状、颜色画出相似的图形
    import matplotlib.pyplot as plt
    from wordcloud import WordCloud, ImageColorGenerator
    import numpy as np
    import PIL.Image as Image
    import os
    
    # d = os.path.dirname(__file__)
    # 可选:coloring = np.array(Image.open(os.path.join(d, '/imgs/wechat.jpg)))
    coloring = np.array(Image.open("{0}/imgs/wechat.jpg".format(os.getcwd()))) # 这里的wechat.jpg可以换成其他你想呈现的效果底图
    my_wordcloud = WordCloud(background_color="white", max_words=2000,
                             mask=coloring, max_font_size=60, random_state=42, scale=2,
                             font_path="/Library/Fonts/songti.ttc").generate(word_space_split)
    
    image_colors = ImageColorGenerator(coloring)
    plt.imshow(my_wordcloud.recolor(color_func=image_colors))
    plt.imshow(my_wordcloud)
    plt.axis("off")
    plt.show()
    
    # 保存图片,并发送到手机
    my_wordcloud.to_file(os.path.join(os.getcwd(), 'output/wechat_signature_cloud.jpg'))
    itchat.send_image('output/wechat_signature_cloud.jpg', 'filehelper')
    

    <div style="text-align: center;">

    底图1:

    词云效果图1:

    底图2:

    词云效果图2:

    </div>

    本文同时发布于个人微信公众号微信好友初步分析

    相关文章

      网友评论

        本文标题:微信好友初步分析

        本文链接:https://www.haomeiwen.com/subject/hbiidxtx.html