Table of Contents
问题说明
我们可能会因为好友过多、群聊过多、经营多个微信号等原因,需要使用程序对微信进行批量处理甚至自动化。在python中itchat是一个在程序控制微信方面经常被提起的第三方库,也有其他在这个库上进行封装的其他第三方库(比如wxpy)
在根据itchat的官方文档进行微信登录时,程序报错,登录失败。
登录代码如下:
1import itchat 2 3itchat.auto_login() 4 5itchat.send('Hello, filehelper', toUserName='filehelper')
运行上述代码,程序将抛出错误:
1site-packages\itchat\components\login.py in login(self, enableCmdQR, picDir, qrCallback, loginCallback, exitCallback)
2 46 isLoggedIn = False
3...
4--> 223 parser.Parse(string, True)
5 224 self._setup_subset(string)
6 225 except ParseEscape:
7
8ExpatError: mismatched tag: line 64, column 4
这是因为程序请求登录微信返回的内容不符合xml格式,大概率是因为微信拒绝了登录请求,返回了一个拒绝提示的html页面,导致xml解析错误:
1<!DOCTYPE html>
2<html lang="en">
3 <head>
4 <meta charset="utf-8">
5 <meta http-equiv="X-UA-Compatible" content="IE=edge">
6 <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, viewport-fit=cover" />
7 <meta name="color-scheme" content="light dark">
8 <title>微信网页版</title>
9 </head>
10 <body>
11 <div class="msg">
12 <div class="msg-text-area">
13 <h2 class="msg-title">提示</h2>
14 <p class="msg-desc">由于安全原因,此微信号不能使用网页版微信。你可以前往微信官网 https://weixin.qq.com/ 下载客户端登录。</p>
15 </div>
16 </div>
17 <script src="//res.wx.qq.com/t/wx_fed/webwx/res/static/vendor/vendor_bb4e225.js"></script><script>webpackJsonp([0],[function(n,exports){n.exports=""}]);/* vhtml-webpack-plugin version: 0.1.38 */</script></body>
18</html>
解决方案
尝试安装了itchat-uos仍然报错,但是安装了指定版本的itchat-uos-1.5.0.dev0后登录成功(可以考虑把itchat直接uninstall掉)。推测可能是因为版本兼容问题或版本功能差异导致,有兴趣的极客朋友可以自行细究。
继续写代码去了~
好书推荐:
好课推荐:
写文不易,如果对你有帮助的话,来一波点赞、收藏、关注吧~👇
