爬取微信好友男女比例

環(huán)境:win10+python3.6

1.python抓取微信好友數(shù)量及好友比例

使用itchat庫(kù)對(duì)微信進(jìn)行操作

登錄微信,跳出一個(gè)二維碼,掃描即可登錄成功

itchat.login()

但是存在的問(wèn)題就是:每次運(yùn)行都需要掃碼登錄,故可以使用另一個(gè)方法

itchat.auto_login(hotReload=True)

除第一次登陸需要掃碼外,后面只需要在手機(jī)確定登陸就行

獲取自己好友的相關(guān)信息,得到一個(gè)json數(shù)據(jù)返回

data=itchat.get_friends(update=True)

打印之后,發(fā)現(xiàn)有很多數(shù)據(jù)信息,如備注,昵稱,城市信息,簽名等。

通過(guò)sex鍵得到好友男女?dāng)?shù)量及比例

 itchat.login()
 text = dict()
 numbers=itchat.get_friends(update=True)
 print(len(numbers))
 friedns = itchat.get_friends(update=True)[0:]
 print(numbers)
 print(friedns)
 male = "male"
 female = "female"
 other = "other"
 for i in friedns[1:]:
 sex = i['Sex']
 if sex == 1:
 text[male] = text.get(male, 0) + 1
 elif sex == 2:
 text[female] = text.get(female, 0) + 1
 else:
 text[other] = text.get(other, 0) + 1
 total = len(friedns[1:])
 print('男性好友數(shù)量:',text[male])
 print('女性好友數(shù)量:',text[female])
 print('未知性別好友數(shù)量:',text[other])
 print("男性好友比例: %.2f%%" % (float(text[male]) / total * 100) + "\n" +
 "女性好友比例: %.2f%%" % (float(text[female]) / total * 100) + "\n" +
?
 "不明性別好友比例: %.2f%%" % (float(text[other]) / total * 100))

最后得到自己好友情況如下

image.png

使用matplotlib庫(kù)來(lái)實(shí)現(xiàn)微信好友男女比例柱狀圖顯示

from matplotlib import pyplot as plt
?
?
def draw(datas):
 for key in datas.keys():
 plt.bar(key, datas[key])
?
 plt.legend()
 plt.xlabel('sex')
 plt.ylabel('rate')
 plt.title("Gender of Alfred's friends")

圖形顯示如下

image.png

這里柱狀圖顯示卻沒(méi)有顯示具體數(shù)值,查閱之后發(fā)現(xiàn)可以使用plt.text()來(lái)實(shí)現(xiàn)在柱狀圖的上方顯示數(shù)值。

具體的代碼修改如下

def draw(datas):
 b=[]
 for key in datas.keys():
 plt.bar(key, datas[key])# 柱狀圖
 b.append(datas[key])
?
 a = datas.keys()
 # 使用plt.text()來(lái)實(shí)現(xiàn)柱狀圖顯示數(shù)值
 for x, y in zip(a, b):
 plt.text(x, y, str(y), ha='center', va='bottom', fontsize=11)
?
 plt.legend()
 plt.xlabel('sex')
 plt.ylabel('rate')
 plt.title("Gender of Alfred's friends")
 plt.show()

修改后的圖形顯示

image.png

項(xiàng)目地址為:

github

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 如果可以,真希望我們沒(méi)有認(rèn)識(shí)過(guò),起碼現(xiàn)在不會(huì)那么落寞。 今天加班,老板把我捎到徐家匯,看時(shí)間已經(jīng)過(guò)八點(diǎn)了,想著不早...
    陌路狂貓閱讀 238評(píng)論 0 0
  • 配置債券型基金降風(fēng)險(xiǎn) 股票投資的不確定性因素比較多,投資者應(yīng)考慮將資金轉(zhuǎn)入低風(fēng)險(xiǎn)的債券基金或銀行理財(cái)產(chǎn)品進(jìn)行避險(xiǎn)。...
    覃若思閱讀 307評(píng)論 0 0
  • 簡(jiǎn)書的第一篇文章,記錄一下今天上午參加徐珂老師的微信面試感想。 第一,當(dāng)老師詢問(wèn)為什么想要參加這份工作時(shí),我回答了...
    a582309閱讀 530評(píng)論 0 0

友情鏈接更多精彩內(nèi)容