美文网首页有趣的python
用python登录WeChat 实现自动回复(非常详细)

用python登录WeChat 实现自动回复(非常详细)

作者: c067527d47c2 | 来源:发表于2019-05-28 15:17 被阅读6次

    如要抄袭 麻烦备注好原文出处!!!
    http://www.cnblogs.com/Wang-jialu/p/10936414.html

    最近实现了一些微信的简单玩法 我们可以通过网页版的微信 微信网页版 ,扫码登录后去抓包爬取信息,还可以post去发送信息。

    image

    当然在学习Python的道路上肯定会困难,没有好的学习资料,怎么去学习呢? 学习Python中有不明白推荐加入交流群号:984137898 群里有志同道合的小伙伴,互帮互助, 群里有不错的视频学习教程和PDF!

    》》安装itchat这个库 pip install itchat

    先来段简单的试用,实现微信的登录,运行下面代码会生成一个二维码,扫码之后手机端确认登录,就会发送一条信息给‘filehelper’,这个 filehelper 就是微信上的文件传输助手。

    <pre class="prettyprint hljs python" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; border: none; overflow-x: auto; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(246, 246, 246);">import itchat

    登录

    itchat.login()

    发送消息

    itchat.send(u'你好鸭!', 'filehelper')
    </pre>

    它会给这个文件传输助手自动发送你好鸭!

    除了登录和发送消息我们还可以这么来玩,往下走~

    》》实现 微信好友男女比例

    想统计下自己微信里好友的性别比例,当然也是很简单,先获取好友列表,统计列表里性别计数

    <pre class="prettyprint hljs python" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; border: none; overflow-x: auto; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(246, 246, 246);">import itchat

    先登录

    itchat.login()

    获取好友列表

    friends = itchat.get_friends(update=True)[0:]

    初始化计数器,有男有女,当然,有些人是不填的

    male = female = other = 0

    遍历这个列表,列表里第一位是自己,所以从"自己"之后开始计算

    1表示男性,2女性

    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:])

    好了,打印结果

    print (u"男性好友:%.2f%%" % (float(male) / total * 100))
    print (u"女性好友:%.2f%%" % (float(female) / total * 100))
    print (u"其他:%.2f%%" % (float(other) / total * 100))
    </pre>

    运行结果:

    image

    》》实现 微信自动回复

    接着来实现一个类似qq上的自动回复,原理就是接收到消息,就发消息回去,同时发一条给文件助手,就可以在文件助手中统一查看消息。

    代码很简单,来看看

    <pre class="prettyprint hljs python" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; border: none; overflow-x: auto; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(246, 246, 246);">#coding=utf8
    import itchat

    自动回复

    封装好的装饰器,当接收到的消息是Text,即文字消息

    @itchat.msg_register('Text')
    def text_reply(msg):
    # 当消息不是由自己发出的时候
    if not msg['FromUserName'] == myUserName:
    # 发送一条提示给文件助手
    itchat.send_msg(u"[%s]收到好友@%s 的信息:%s\n" %
    (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(msg['CreateTime'])),
    msg['User']['NickName'],
    msg['Text']), 'filehelper')
    # 回复给好友
    return u'[自动回复]您好,我现在有事不在,一会再和您联系。\n已经收到您的的信息:%s\n' % (msg['Text'])

    if name == 'main':
    itchat.auto_login()

    # 获取自己的UserName
    myUserName = itchat.get_friends(update=True)[0]["UserName"]
    itchat.run()
    

    </pre>

    运行后会保持登录状态,开启自动回复模式,手机上查看:

    image image

    当然,除了文字Text信息,还可以接收图片(表情包算图片),语音,名片,地理位置,分享和类型为Note的信息(就是有人提示类的消息,例如撤回消息),把装饰器写成下面形式即可实现

    <pre class="prettyprint hljs css" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; border: none; overflow-x: auto; background-color: rgb(246, 246, 246);">@itchat.msg_register(['Map', 'Card', 'Note', 'Sharing', 'Picture','Text'])
    </pre>

    image

    相关文章

      网友评论

        本文标题:用python登录WeChat 实现自动回复(非常详细)

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