Python玩微信(1):初探wxpy
1.前期准备
wxpy项目主页里面有它的相关介绍
pyecharts项目主页,是python与百度echarts的桥梁,我用来做数据分析
2.查看微信好友男女比例
from wxpy import *from pyecharts import Piebot = Bot(cache_path = True) #定义一个微信机器人friends = bot.friends(update=False) #获取更新好友列表male = female = other = 0 for i in friends[1:]: #[1:]是因为整个好友列表里面自己市在第一个,排除掉 sex = i.sex if sex == 1: male += 1 elif sex == 2: female += 1 else: other += 1total = 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.查看好友地区分布
from wxpy import *from pyecharts import Map#因为获取的列表城市都没有带市字,而pyecharts需要带个市字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('市')#把字典进行有序拆分为2个列表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
我微信里面潮州多的原因就是我是潮州人啊,然后广州多的原因可能就是我在广州读书吧,很多人大学才玩微信的,如果是那时候定位,就直接定位为广州了
这个图要GIF观看才好看点,可怜我的七牛云流量-。-(免费版只有10G可以用啊!!!有人看一次就1M没了)
4.查看好友签名,并利用jieba分词,再制作成词云
from wxpy import *import reimport jiebaimport matplotlib.pyplot as pltfrom wordcloud import WordCloudimport PIL.Image as Imagebot = Bot(cache_path = True)friends = bot.friends(update=False)male = female = other = 0#提取好友签名,并去掉span,class,emoji,emoji1f3c3等的字段signatures = []for i in friends: signature = i.signature.strip().replace("span", "").replace("class", "").replace("emoji", "")# 正则匹配过滤掉emoji表情,例如emoji1f3c3等 rep = re.compile("1f\d.+") signature = rep.sub("", signature) signatures.append(signature)# 拼接字符串text = "".join(signatures)# jieba分词wordlist_jieba = jieba.cut(text, cut_all=True)wl_space_split = " ".join(wordlist_jieba)# wordcloud词云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
赞 (0)