国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

itchat和matplotlib的結(jié)合使用爬取微信信息的實(shí)例

2020-02-16 02:08:40
字體:
供稿:網(wǎng)友

前幾天無意中看到了一片文章,《用 Python 爬了爬自己的微信朋友(實(shí)例講解)》,這篇文章寫的是使用python中的itchat爬取微信中朋友的信息,其中信息包括,昵稱、性別、地理位置等,然后對這些信息進(jìn)行統(tǒng)計(jì)并且以圖像形式顯示。文章對itchat的使用寫的很詳細(xì),但是代碼是貼圖,畫圖使用R中的包畫,我對著做了一遍,并且把他沒有貼畫圖的代碼做了一遍,畫圖是使用matplotlib。由于他沒有貼代碼,所以我把我寫的貼出來供以后復(fù)制。

首先是安裝itchat的包,可以使用清華大學(xué)的鏡像:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple itchat

爬取微信好友男女比例:

import itchat itchat.login()friends=itchat.get_friends(update=True)[0:]male=female=other=0for i in friends[1:]: sex=i['Sex'] if sex==1:  male+=1 elif sex==2:  female+=1 else:  other+=1   total=len(friends[1:])malecol=round(float(male)/total*100,2)femalecol=round(float(female)/total*100,2)othercol=round(float(other)/total*100,2)print('男性朋友:%.2f%%' %(malecol)+'/n'+'女性朋友:%.2f%%' % (femalecol)+'/n'+'性別不明的好友:%.2f%%' %(othercol))print("顯示圖如下:")

 畫圖:柱狀圖和餅狀圖,圖片如下:

import numpy as npimport matplotlib.pyplot as pltimport matplotlib as mpl#解決中文亂碼不顯示問題mpl.rcParams['font.sans-serif'] = ['SimHei'] #指定默認(rèn)字體 mpl.rcParams['axes.unicode_minus'] = False #解決保存圖像是負(fù)號'-'顯示為方塊的問題  map = { 'Female': (malecol, '#7199cf'), 'Male': (femalecol, '#4fc4aa'), 'other': (othercol, '#e1a7a2')} fig = plt.figure(figsize=(5,5))# 整體圖的標(biāo)題ax = fig.add_subplot(111)#添加一個子圖ax.set_title('Gender of friends') xticks = np.arange(3)+0.15# 生成x軸每個元素的位置bar_width = 0.5# 定義柱狀圖每個柱的寬度names = map.keys()#獲得x軸的值values = [x[0] for x in map.values()]# y軸的值colors = [x[1] for x in map.values()]# 對應(yīng)顏色 bars = ax.bar(xticks, values, width=bar_width, edgecolor='none')# 畫柱狀圖,橫軸是x的位置,縱軸是y,定義柱的寬度,同時設(shè)置柱的邊緣為透明ax.set_ylabel('Proprotion')# 設(shè)置標(biāo)題ax.set_xlabel('Gender')ax.grid()#打開網(wǎng)格ax.set_xticks(xticks)# x軸每個標(biāo)簽的具體位置ax.set_xticklabels(names)# 設(shè)置每個標(biāo)簽的名字ax.set_xlim([bar_width/2-0.5, 3-bar_width/2])# 設(shè)置x軸的范圍ax.set_ylim([0, 100])# 設(shè)置y軸的范圍for bar, color in zip(bars, colors): bar.set_color(color)# 給每個bar分配指定的顏色 height=bar.get_height()#獲得高度并且讓字居上一點(diǎn) plt.text(bar.get_x()+bar.get_width()/4.,height,'%.2f%%' %float(height))#寫值plt.show()#畫餅狀圖fig1 = plt.figure(figsize=(5,5))# 整體圖的標(biāo)題ax = fig1.add_subplot(111)ax.set_title('Pie chart')labels = ['{}/n{} %'.format(name, value) for name, value in zip(names, values)]ax.pie(values, labels=labels, colors=colors)#并指定標(biāo)簽和對應(yīng)顏色plt.show()            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 延寿县| 涟源市| 钦州市| 方城县| 阿拉善盟| 乐清市| 区。| 唐山市| 武胜县| 宣城市| 全州县| 塔城市| 翁牛特旗| 贵溪市| 东明县| 岳阳市| 寻乌县| 当涂县| 大连市| 改则县| 四会市| 南靖县| 靖宇县| 上思县| 元阳县| 扶余县| 永丰县| 米泉市| 塘沽区| 焦作市| 安泽县| 钦州市| 广昌县| 盱眙县| 长子县| 昌宁县| 永丰县| 封开县| 通江县| 孟连| 龙南县|