Mac:itchat实现微信自动回复及解决乱码问题
- 1)无法登陆wechat
- 2)output乱码
今天出于无聊,实现了一下用wechat生成自动回复的功能,关于code很多博主都有现成的code提供,然后我copy了下面链接。
https://www.cnblogs.com/Wang-jialu/p/10936414.html
但是在实现的过程中,首先是报错,ascii报错如下:
‘ascii’ codec can’t encode characters in position 31-59: ordinal not in range(128) File “/Users/echo/Desktop/wechatauto.gyp”, line 30, in itchat.auto_login()
意思是存在了非法的ascii字符,因为ascii字符不能表示中文汉字,解决方法:
1
2
3
4import sys reload(sys) sys.setdefaultencoding('utf-8')
然后说一下在实现过程中遇到的几个小问题和解决方法:
1)无法登陆wechat
our wechat account may be LIMITED to log in WEB wechat, error info:
1203为了你的帐号安全,此微信号已不允许登录网页微信。你可以使用Windows微信或Mac微信在电脑端登录。Windows微信下载地址:https://pc.weixin.qq.com Mac微信下载地址:https://mac.weixin.qq.com
我当时用的是一个新的号,新号好像是一般都不能登陆,尝试换一个号吧
2)output乱码
一般是在code的过程中编码type弄错了,原链接中代码我跑的时候是乱码的,代码修改如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28import itchat import sys reload(sys) sys.setdefaultencoding('utf-8') # 先登录 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 ("男性好友:%.2f%%" % (float(male) / total * 100)).decode('utf-8') print ("女性好友:%.2f%%" % (float(female) / total * 100)).decode('utf-8') print ("其他:%.2f%%" % (float(other) / total * 100)).decode('utf-8')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26import itchat import sys import time reload(sys) sys.setdefaultencoding('utf-8') #coding=utf8 import itchat # 自动回复 # 封装好的装饰器,当接收到的消息是Text,即文字消息 @itchat.msg_register('Text') def text_reply(msg): # 当消息不是由自己发出的时候 if not msg['FromUserName'] == myUserName: # 发送一条提示给文件助手 itchat.send_msg("[%s]收到好友@%s 的信息:%sn".decode('utf-8') % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(msg['CreateTime'])), msg['User']['NickName'], msg['Text']), 'filehelper') # 回复给好友 return '[自动回复]已经收到您的的信息,明年有空就回哦~:%sn'.decode('utf-8') % (msg['Text']) if __name__ == '__main__': itchat.auto_login() # 获取自己的UserName myUserName = itchat.get_friends(update=True)[0]["UserName"] itchat.run()
主要是对编码的一些转换
主要使用的编码类型有unicode,utf-8(linux),gbk(Windows)…
这个链接里面的介绍和解释很全:
https://blog.csdn.net/joyfixing/article/details/79971667
&
https://www.cnblogs.com/shadowhu/p/6553726.html
一只小白再次谢谢大佬~
最后
以上就是细腻钻石最近收集整理的关于Mac:itchat实现微信自动回复及解决乱码问题的全部内容,更多相关Mac内容请搜索靠谱客的其他文章。
发表评论 取消回复