在网上发现了wxpy这个库,可以调取一些微信API来玩转微信,本文主要是在提取微信好友数据
Python玩微信(1):初探wxpy 1.前期准备 wxpy项目主页 里面有它的相关介绍pyecharts项目主页 ,是python与百度echarts的桥梁,我用来做数据分析
2.查看微信好友男女比例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 from wxpy import *from pyecharts import Pie bot = Bot(cache_path = True ) friends = bot.friends(update=False ) male = female = other = 0 for i in friends[1 :]: sex = i.sex if sex == 1 : male += 1 elif sex == 2 : female += 1 else : other += 1 total = len (friends[1 :]) attr = ["男性" ,"女性" ,"其他" ] v1 = [float (male),float (female),float (other)] pie = Pie("饼图-圆环图示例" , title_pos='center' ) pie.add("" , attr, v1, radius=[40 , 75 ], label_text_color=None , is_label_show=True , legend_orient='vertical' , legend_pos='left' ) pie.render("sex.html" )
结果输出如图enter description here 没想到我微信里面女性好友是男性好友的一半-。- , 可能是跟我大学专业有关吧
3.查看好友地区分布 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 from wxpy import *from pyecharts import Map b = '市' def s (x ): return x+b bot = Bot(cache_path = True ) friends = bot.friends(update=False ).search(province = '广东' ) citys = [] for f in friends : city = f.city citys.append(city) r = map (s,citys) cityss = list (r) a = {}for i in cityss: a[i] = cityss.count(i) a.pop('市' ) attrs = [] values = []for value, attr in a.items(): values.append(attr) attrs.append(value)map = Map("广东地图示例" , width=1200 , height=600 )map .add("" , attrs, values, maptype='广东' , is_visualmap=True , visual_text_color='#000' )map .render("city.html" )
数据呈现如下:enter description here 我微信里面潮州多的原因就是我是潮州人啊,然后广州多的原因可能就是我在广州读书吧,很多人大学才玩微信的,如果是那时候定位,就直接定位为广州了
4.查看好友签名,并利用jieba分词,再制作成词云 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 from wxpy import *import reimport jiebaimport matplotlib.pyplot as plt from wordcloud import WordCloudimport PIL.Image as Imagebot = Bot(cache_path = True)friends = bot.friends(update=False) male = female = other = 0 signatures = [] for i in friends: signature = i.signature.strip().replace("span" , "" ).replace("class" , "" ).replace("emoji" , "" ) rep = re.compile("1f\d.+" ) signature = rep.sub("" , signature) signatures.append(signature)text = "" .join(signatures)wordlist_jieba = jieba.cut(text, cut_all=True) wl_space_split = " " .join(wordlist_jieba)my_wordcloud = WordCloud(background_color="white", max_words=2000, max_font_size=1000, random_state=42, font_path='./hanyi.ttf').generate(wl_space_split) plt.imshow(my_wordcloud) plt.axis("off" ) plt.show()
结果显示如图:enter description here 结果可以发现,我好友里面最多的就是“努力”了,其次是“自己”,“一个”,“可以,一生”。还有“生活,成为,半生,喜欢,当下,轮滑,珍惜” 哈哈哈,出现轮滑是因为我好友列表里面很多都是学校里面的轮滑协会的。不过出现代理就。。。。。。(捂脸)