美文网首页python
Python玩微信(1):初探wxpy

Python玩微信(1):初探wxpy

作者: 陈思煜 | 来源:发表于2017-08-15 14:50 被阅读3525次

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 hereenter 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 hereenter 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 hereenter description here

结果可以发现,我好友里面最多的就是“努力”了,其次是“自己”,“一个”,“可以,一生”。还有“生活,成为,半生,喜欢,当下,轮滑,珍惜”
哈哈哈,出现轮滑是因为我好友列表里面很多都是学校里面的轮滑协会的。不过出现代理就。。。。。。(捂脸)

相关文章

  • Python玩微信(1):初探wxpy

    Python玩微信(1):初探wxpy 1.前期准备 wxpy项目主页里面有它的相关介绍pyecharts项目主页...

  • Python也能聊微信

    最近研究微信API,发现个非常好用的python库:wxpy。wxpy基于itchat,使用了 Web 微信的通讯...

  • Python也能聊微信

    最近研究微信API,发现个非常好用的python库:wxpy。wxpy基于itchat,使用了 Web 微信的通讯...

  • python制作微信聊天机器人

    最近研究微信API,发现个非常好用的python库:wxpy。wxpy基于itchat,使用了 Web 微信的通讯...

  • Python也能聊微信

    最近研究微信API,发现个非常好用的python库:wxpy。wxpy基于itchat,使用了 Web 微信的通讯...

  • [转]wxpy: 用 Python 玩微信

    github上wxpy网址优雅的微信个人号 机器人/API,基于 itchat,全面优化接口,更有 Python ...

  • Python玩微信(2):wxpy的进阶

    Python玩微信(2):wxpy的进阶 前记:其实我也不知道这个算不算进阶- -。期初目的是因为我脑海里面有一个...

  • 自动回复与好友获取

    今天初学了python的微信自动回复机器人课程。首先是利用导入python包wxpy。使用cmdpip insta...

  • Python学习之机器人自动回复好友信息

    1、需要用的的库:from wxpy import *wxpy(微信机器人)是在itchat基础上开发的微信个人功...

  • 微信机器人

    Python微信机器人 安装方法:pip3 install -U wxpy上文中示例,有图标分析,如果有报错的话,...

网友评论

  • mo_陌上花开:大佬,你知道这个错误不 KeyError: 'pass_ticket',微信登不上网页版的,该怎么解决
    东东隆东抢:一模一样的异常,估计是微信平台做了处理。
    mo_陌上花开:@陈思煜 Getting uuid of QR code.
    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'
    陈思煜:@mo_陌上花开 完整异常有吗?
  • ibo:公众号点击菜单后发消息怎么弄?
  • cac6373fa4d4:Traceback (most recent call last):
    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)
  • f649168bcc47:请问,a.pop('市'),大佬的意思是要把字典里的‘市’字去掉吗?
    480a814ccf9a:@MartinLee_f521 楼主这个不是要去掉所有的市字,只是去掉那些只有市字的字典条目
    陈思煜:@MartinLee_f521 你应该先查一查有没有数据 再根据数据决定的
  • 榴莲气象:试了下后面两个脚本都有报错:

    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.
    陈思煜:@grug350 你尝试输出下 看有没有什么东西输出 可能腾讯更新后 结果从深圳市-->深圳了

本文标题:Python玩微信(1):初探wxpy

本文链接:https://www.haomeiwen.com/subject/tyxvrxtx.html