大数据,机器学习,人工智能python模块Python

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

2019-05-28  本文已影响0人  A遇上方知友

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

》》安装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; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import itchat

如果有想要学习Python或者正在学习Python中的小伙伴,需要学习资料的话,可以到我的微信公众号:Python学习知识圈,后台回复:“01”,即可拿Python学习资料

登录

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; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#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; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">@itchat.msg_register(['Map', 'Card', 'Note', 'Sharing', 'Picture','Text'])
</pre>

image

完成!

上一篇下一篇

猜你喜欢

热点阅读