Python玩微信(1):初探wxpy
1.前期准备
wxpy项目主页里面有它的相关介绍
pyecharts项目主页,是python与百度echarts的桥梁,我用来做数据分析
2.查看微信好友男女比例
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:]: #[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.查看好友地区分布
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 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()
结果显示如图:
enter description here
结果可以发现,我好友里面最多的就是“努力”了,其次是“自己”,“一个”,“可以,一生”。还有“生活,成为,半生,喜欢,当下,轮滑,珍惜”
哈哈哈,出现轮滑是因为我好友列表里面很多都是学校里面的轮滑协会的。不过出现代理就。。。。。。(捂脸)










网友评论
Downloading QR code.
Please scan the QR code to log in.
Please press confirm on your phone.
Loading the contact, this may take a little while.
Traceback (most recent call last):
File "D:/python/201806/weixin-1.0/weixin.py", line 168, in <module>
the_push()
File "D:/python/201806/weixin-1.0/weixin.py", line 21, in the_push
local_index()
File "D:/python/201806/weixin-1.0/weixin.py", line 33, in local_index
i = all_push()
File "D:/python/201806/weixin-1.0/weixin.py", line 74, in all_push
bot = Bot()
File "C:\Python36\lib\site-packages\wxpy\api\bot.py", line 86, in __init__
loginCallback=login_callback, exitCallback=logout_callback
File "C:\Python36\lib\site-packages\itchat\components\register.py", line 35, in auto_login
loginCallback=loginCallback, exitCallback=exitCallback)
File "C:\Python36\lib\site-packages\itchat\components\login.py", line 66, in login
self.show_mobile_login()
File "C:\Python36\lib\site-packages\itchat\components\login.py", line 212, in show_mobile_login
self.loginInfo['url'], self.loginInfo['pass_ticket'])
KeyError: 'pass_ticket'
File "E:\robot.py", line 23, in <module>
adminer = bot.friends(update=True).search(admin_request_name)[0]
File "C:\Python27\lib\site-packages\wxpy\api\chats\chats.py", line 50, in search
return Chats(filter(match, self), self.source)
File "C:\Python27\lib\site-packages\wxpy\api\chats\chats.py", line 44, in match
if not match_name(chat, keywords):
File "C:\Python27\lib\site-packages\wxpy\utils\misc.py", line 187, in match_name
if kw in '{0}'.format(getattr(chat, attr, '')).lower():
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
Please press confirm on your phone.
INFO:itchat:Please press confirm on your phone.
Loading the contact, this may take a little while.
INFO:itchat:Loading the contact, this may take a little while.
Login successfully as
INFO:itchat:Login successfully as
{}
Traceback (most recent call last):
File "test-city.py", line 24, in <module>
a.pop('市')
KeyError: '市'
LOG OUT!
INFO:itchat:LOG OUT!
---------------------------------------------------------
Login successfully as
Traceback (most recent call last):
File "test-ciyun.py", line 32, in <module>
font_path='hanyi.ttf').generate(wl_space_split)
File "/public/home/hysplit/software/anaconda3/lib/python3.6/site-packages/wordcloud/wordcloud.py", line 571, in generate
return self.generate_from_text(text)
File "/public/home/hysplit/software/anaconda3/lib/python3.6/site-packages/wordcloud/wordcloud.py", line 553, in generate_from_text
self.generate_from_frequencies(words)
File "/public/home/hysplit/software/anaconda3/lib/python3.6/site-packages/wordcloud/wordcloud.py", line 351, in generate_from_frequencies
"got %d." % len(frequencies))
ValueError: We need at least 1 word to plot a word cloud, got 0.