python探索微信朋友信息!

作者: 14e61d025165 | 来源:发表于2019-06-16 15:57 被阅读1次

    一、itchat

    itchat是一个开源的微信个人号接口,这一次就用它来来玩玩。

    在使用之前,先下载,老规矩通过 pip install itchat 即可安装。

    想要获取朋友圈信息,只需要几行代码就可以获取。为减少登录次数,将获取到的信息保存到 json 文件中即可。

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">itchat.login() # 通过二维码连接登录账号Python学习交流群:1004391443
    friend_msg = itchat.get_friends(update=True)[0:] # 获取微信好友信息

    将微信好友信息保存起来,减少登录的次数

    with open('./friend_message.json', 'w', encoding='utf-8') as file:
    json.dump(friend_msg, file, ensure_ascii=False)
    file.close()
    </pre>

    二、读取文件获取信息

    我们只需要关注里面其中的主要信息,按照需求获取。由于只是玩玩而已,就只单单获取性别和城市信息。

    先获取性别信息

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def get_gender(message):
    sex_dic = {}
    sex_list = []
    for i in range(1, len(message)):
    sex = message[i]['Sex']
    if sex == 1:
    sex_dic['Male'] = sex_dic.get('Male', 0) + 1
    sex_list.append("男")
    elif sex == 2:
    sex_dic['Female'] = sex_dic.get('Female', 0) + 1
    sex_list.append("女")
    else:
    sex_dic['Unknown'] = sex_dic.get('Unknown', 0) + 1
    sex_list.append("Unknown")
    return sex_dic, sex_list
    </pre>

    再获取城市信息

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def get_city(message):
    city_list = []
    for i in range(1, len(message)):
    city = message[i]['City']
    if city == '':
    city_list.append(None)
    else:
    city_list.append(city)
    return city_list
    </pre>

    三、可视化

    将性别绘制成饼状图,城市分布绘制成柱状图。具体绘制的代码就不上了,详情请前往 GitHub

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">with open("./friend_message.json", 'r', encoding='utf-8') as file:
    f_msg = json.load(file)
    file.close()
    sex_dic, sex_list = getData.get_gender(f_msg)
    city_list = getData.get_city(f_msg)

    将三个属性组成 DataFrame

    data = pd.DataFrame({'gender': sex_list, 'city': city_list})

    获取城市前十的数量

    city_dict = data['city'].value_counts()[:15].to_dict()

    绘制性别环状图

    draw_pie(sex_dic)

    绘制城市柱状图

    draw_bar(city_dict)
    </pre>

    四、总结

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1560671747379" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    说实话,这里面的男生绝对大部分是在大学认识的,毕竟理工科学校可不是闹着玩儿的;还有这 6.58% 不明性别的人不知道怎么设置的,为什么我没办法不设置,还是说......

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1560671747385" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    作为一个在东莞上学的广东汕头人,这样的分布确实是在意料之中。毕竟汕头是我生活了那么久的地方,在东莞也快度过三个年头了,不知不觉要大四了,最近的压力已经逐渐增加了,无力吐槽。

    这只是玩玩而已,如果你有什么脑洞或者想要挖掘更多个人好友信息,可以使用 itchat 接着玩。

    相关文章

      网友评论

        本文标题:python探索微信朋友信息!

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