天天看點

如何用Python擷取微信好友資訊,讓你更加了解你的朋友

前言

文的文字及圖檔來源于網絡,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯系我們以作處理。

作者: 吳小鵬

PS:如有需要Python學習資料的小夥伴可以加點選下方連結自行擷取

python免費學習資料以及群交流解答點選即可加入

「itchat」(開源的微信個人接口)來擷取一些公開的好友資訊,使用itchat.get_friends(),

我們可以擷取微信好友頭像、性别、省份、城市、年齡等具體資訊。

好友頭像

我們先拉取好友頭像試一下,「itchat」裡的get_head_img可以擷取每個好友的頭像:

def headImg():
    itchat.login()
    friends = itchat.get_friends(update=True)
    # itchat.get_head_img() 擷取到頭像二進制,并寫入檔案,儲存每張頭像
    for count, f in enumerate(friends):
        # 根據userName擷取頭像
        img = itchat.get_head_img(userName=f["UserName"])
        imgFile = open("photo/" + str(count) + ".jpg", "wb")
        imgFile.write(img)
        imgFile.close()
           

photo檔案夾用于儲存頭像圖檔,周遊好友清單,根據下标命名頭像,到這裡可以看到檔案夾裡已經儲存了所有好友的頭像。

頭像資訊比較隐私,就不公布啦~

性别分布

當我們使用「itchat」的get_friends()函數可以擷取很多好友資訊,包括性别,是以這裡隻需要将擷取到的好友性别資訊提取出來制圖就可以:

def analysisSex():
    itchat.login()
    friends = itchat.get_friends()
    sex_count = dict()
    for f in friends:
        if f["Sex"] == 1:  # man
            sex_count["man"] = sex_count.get("man", 0) + 1
        elif f["Sex"] == 2:  # women
            sex_count["women"] = sex_count.get("women", 0) + 1
        else:  # unknown
            sex_count["unknown"] = sex_count.get("unknown", 0) + 1
    # 柱狀圖展示
    for i, key in enumerate(sex_count):
        plt.bar(key, sex_count[key])
    plt.savefig("analysisSex.png") #儲存圖檔
    plt.ion()
    plt.close()
           

結果如下:

如何用Python擷取微信好友資訊,讓你更加了解你的朋友

個性簽名

在擷取的好友資訊中Signature字段對應着好友的簽名,我們直接擷取這部分資訊,然後儲存下來,處理過表情等特殊字元,然後制作詞雲圖。

def AnalysisSignature():
    itchat.login()
    friends = itchat.get_friends(update=True)
    file = open('AnalysisSignature.txt', 'a', encoding='utf-8')
    for f in friends:
        signature = f["Signature"].strip().replace("emoji", "").replace("span", "").replace("class", "")
        # 正則比對
        rec = re.compile("1f\d+\w*|[<>/=]")
        signature = rec.sub("", signature)
        file.write(signature + "\n")
           

“stay hungry, stay foolish”

“不舍愛與自由”

如何用Python擷取微信好友資訊,讓你更加了解你的朋友

大家對生活都是積極向上的,希望每個人都能成為更好的自己!

地區分布

為了統計好友的地區分布,是以要用到好友資訊的province字段,直接對province進行統計,然後可視化出來得到。

# 省份分析
def analysisProvince():
    friends_info = get_friends_info()
    df = pd.DataFrame(friends_info)
    province_count = df.groupby('province', as_index=True)['province'].count().sort_values()
    temp = list(map(lambda x: x if x != '' else '未知', list(province_count.index)))
    # 畫圖
    page = Page()
    style = Style(width=1100, height=600)
    style_middle = Style(width=900, height=500)
    attr, value = temp, list(province_count)
    chart1 = Map('好友分布(中國地圖)', **style.init_style)
    chart1.add('', attr, value, is_label_show=True, is_visualmap=True, visual_text_color='#000')
    page.add(chart1)
    chart2 = Bar('好友分布柱狀圖', **style_middle.init_style)
    chart2.add('', attr, value, is_stack=True, is_convert=True,
               label_pos='inside', is_legend_show=True, is_label_show=True)
    page.add(chart2)
    page.render('analysisProvince.html')
           

比較明顯的一個點是,我們的好友大多來自我們生活過的地方,安徽和上海這兩個地區好友數量都明顯高于其他省份。

如何用Python擷取微信好友資訊,讓你更加了解你的朋友

号主戶籍安徽,利用城市資訊看一下我的朋友都在哪裡。

從下面的好友數量來看,号主明顯是個安慶人。

如何用Python擷取微信好友資訊,讓你更加了解你的朋友

你們可以直接修改源碼的省份參數,擷取你想了解的省份的好友分布。