Python玩微信(1):初探wxpy
1.前期準(zhǔn)備
wxpy項目主頁里面有它的相關(guān)介紹
pyecharts項目主頁,是python與百度echarts的橋梁,我用來做數(shù)據(jù)分析
2.查看微信好友男女比例
from wxpy import *
from pyecharts import Pie
bot = Bot(cache_path = True) #定義一個微信機(jī)器人
friends = bot.friends(update=False) #獲取更新好友列表
male = female = other = 0
for i in friends[1:]: #[1:]是因?yàn)檎麄€好友列表里面自己市在第一個,排除掉
sex = i.sex
if sex == 1:
male += 1
elif sex == 2:
female += 1
else:
other += 1
total = len(friends[1:]) #計算總數(shù)
#下面為分析
attr = ["男性","女性","其他"]
v1 = [float(male),float(female),float(other)]
pie = Pie("餅圖-圓環(huán)圖示例", 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")
結(jié)果輸出如圖

enter description here
沒想到我微信里面女性好友是男性好友的一半-。- , 可能是跟我大學(xué)專業(yè)有關(guān)吧
3.查看好友地區(qū)分布
from wxpy import *
from pyecharts import Map
#因?yàn)楂@取的列表城市都沒有帶市字,而pyecharts需要帶個市字
b = '市'
def s(x):
return x+b
#因?yàn)槲液糜牙锩娉藦V東的外和其他的,剩下非廣東的寥寥無幾,所以只提取廣東的
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)
#為城市計數(shù)
a = {}
for i in cityss:
a[i] = cityss.count(i)
a.pop('市')
#把字典進(jìn)行有序拆分為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")
數(shù)據(jù)呈現(xiàn)如下:

enter description here
我微信里面潮州多的原因就是我是潮州人啊,然后廣州多的原因可能就是我在廣州讀書吧,很多人大學(xué)才玩微信的,如果是那時候定位,就直接定位為廣州了
這個圖要GIF觀看才好看點(diǎn),可憐我的七牛云流量-。-(免費(fèi)版只有10G可以用啊!!!有人看一次就1M沒了)
4.查看好友簽名,并利用jieba分詞,再制作成詞云
from wxpy import *
import re
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import PIL.Image as Image
bot = 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()
結(jié)果顯示如圖:

enter description here
結(jié)果可以發(fā)現(xiàn),我好友里面最多的就是“努力”了,其次是“自己”,“一個”,“可以,一生”。還有“生活,成為,半生,喜歡,當(dāng)下,輪滑,珍惜”
哈哈哈,出現(xiàn)輪滑是因?yàn)槲液糜蚜斜砝锩婧芏喽际菍W(xué)校里面的輪滑協(xié)會的。不過出現(xiàn)代理就。。。。。。(捂臉)