「Python|微信机器人|问题记录」使用itchat登录微信失败 mismatched tag 错误

本文主要记录使用python的第三方库itchat进行微信操作自动化时,在第一步进行微信登录时遇到的登录失败问题,以及最后解决问题的方案

文章目录

  • 问题说明
  • 解决方案

问题说明

我们可能会因为好友过多、群聊过多、经营多个微信号等原因,需要使用程序对微信进行批量处理甚至自动化。在pythonitchat是一个在程序控制微信方面经常被提起的第三方库,也有其他在这个库上进行封装的其他第三方库(比如wxpy

在根据itchat的官方文档进行微信登录时,程序报错,登录失败。
登录代码如下:

import itchat

itchat.auto_login()

itchat.send('Hello, filehelper', toUserName='filehelper')

运行上述代码,程序将抛出错误:

site-packages\itchat\components\login.py in login(self, enableCmdQR, picDir, qrCallback, loginCallback, exitCallback)
     46         isLoggedIn = False
...
--> 223             parser.Parse(string, True)
    224             self._setup_subset(string)
    225         except ParseEscape:

ExpatError: mismatched tag: line 64, column 4

这是因为程序请求登录微信返回的内容不符合xml格式,大概率是因为微信拒绝了登录请求,返回了一个拒绝提示的html页面,导致xml解析错误:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, viewport-fit=cover" />
    <meta name="color-scheme" content="light dark">
    <title>微信网页版</title>
  </head>
  <body>
    <div class="msg">
      <div class="msg-text-area">
        <h2 class="msg-title">提示</h2>
        <p class="msg-desc">由于安全原因,此微信号不能使用网页版微信。你可以前往微信官网 https://weixin.qq.com/ 下载客户端登录。</p>
      </div>
          </div>
  <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>
</html>

解决方案

尝试安装了itchat-uos仍然报错,但是安装了指定版本的itchat-uos-1.5.0.dev0后登录成功(可以考虑把itchat直接uninstall掉)。推测可能是因为版本兼容问题或版本功能差异导致,有兴趣的极客朋友可以自行细究。

继续写代码去了~

好书推荐:

  • 流畅的python
  • Python编程 从入门到实践 第2版
  • Python数据结构与算法分析 第2版

好课推荐:

  • 零基础学python
  • python核心技术与实战
  • python自动化办公实战

写文不易,如果对你有帮助的话,来一波点赞、收藏、关注吧~👇

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2023年11月13日
下一篇 2023年11月13日

相关推荐