首页 > 编程语言 >python接收微信消息报'HTMLParser' object has no attribute 'unescape'错误

python接收微信消息报'HTMLParser' object has no attribute 'unescape'错误

时间:2022-09-22 12:22:52浏览次数:98  
标签:unescape itchat python no html HTMLParser import htmlParser

我的博客

一直有个想法,想要弄个微信机器人,然而出师不利,刚开始就碰壁了

先上代码,这个是用来接收消息的,是个测试脚本

#!/usr/bin/python
# coding: utf-8

import itchat

def write_infomation(text_value):
  print(text_value)

@itchat.msg_register(itchat.content.TEXT)
def get_reply(msg):
  write_infomation(msg.text)

itchat.auto_login()
itchat.run()

我连接微信用的itchat

itchat.auto_login()正常跳出登陆二维码,用手机扫码后正常登陆,本来以为一切正常,尝试给自己发了个消息,这时候就弹出错误'HTMLParser' object has no attribute 'unescape',并且,每次发消息,都会弹一段错误出来,证明消息接收没问题,那么就看错误了,提示itchat的utils.py这个文件报错,于是定位到问题点

下面我把这个文件的重点代码放上来

from HTMLParser import HTMLParser

htmlParser = HTMLParser()

d[k]  = htmlParser.unescape(d[k]) #就是这里报错

在网上搜索了一下,这个报错的原因是因为高版本python废弃了HTMLParser().unescape()这个方法,所以提示找不到这个方法,可以用html.unescape()来替代

那么后面的事就简单了,把utils.py这个文件的关键代码替换下就行了

 

from HTMLParser import HTMLParser
import html #首先导入html

htmlParser = HTMLParser()

d[k]  = html.unescape(d[k]) #这里把htmlParser改成html

 

再次执行测试了一下

正常接收消息,那么接下来故事就这么开始了

 

标签:unescape,itchat,python,no,html,HTMLParser,import,htmlParser
From: https://www.cnblogs.com/xiu123/p/16718798.html

相关文章

  • python生成sql server 数据库表名和sql脚本
    #-*-coding:utf-8-*-#作者:TY#时间:2020-08-0522:03#文件名称:sqlserver数据库导表脚本.PY#开发工具:PyCharmimportpymssqlimportosclassSQLServer:......
  • 【Python】python语言基础概述
    1.文件类型python文件有3种类型:(1)源代码文件:xx.py。(2)字节代码文件:xx.pyc。由xx.py编译成的二进制字节码文件,通过Python程序加载执行,速度快,能够隐藏源码......
  • Linux安装tomcat后启动报错Cannot find ./catalina.sh的解决方法
    Linux安装tomcat后启动报错:Cannotfind./catalina.shThefileisabsentordoesnothaveexecutepermissionThisfileisneededtorunthisprogram原因:无权限解......
  • python 爬虫curl
    目录python爬虫curlpython爬虫curl#!/usr/bin/envpython#-*-coding:utf-8-*-#BY:wenchao.Litime:2021/11/18#curl工具:c写的一个命令#-A设置user-agent......
  • python数据处理小工具
    python处理数据常用方法,包括:1)按照指定行数split_size,分割超大csv文件2)读取csv文件数据,并发送http-json请求,订正生产或者测试环境数据3)csv文件按照某一列分割成多个cs......
  • python18篇 环境迁移
    在原来环境中使用pipfreeze>文件名称,这样会生成一个文件,一般命名为requirements.txt在新的环境中使用pipinstall-r文件名(第一步生成的文件),即可下载安装,这样就不会......
  • (No operations allowed after connection closed.). Possibly consider using a shor
    (Nooperationsallowedafterconnectionclosed.).PossiblyconsiderusingashortermaxLifetimevalue 先按提示添加maxLifetime的超时时间试试看spring:#......
  • Redis与Python连接实例
    2022-09-221、Redis与Python建立连接之前需要先安装“Redis”安装包:在ubantu中,打开终端,输入命令:sudopipinstallredis此时,是安装Python2.0的版本。一般来说,是将3......
  • python requests模块获取与使用cookie
    一.处理cookie方法一:用requests.utils.dict_from_cookiejar()把返回的cookies转换成字典importrequestsdeflogin():url='http://www.xxx.com/login'......
  • How to exit Node.js REPL environment All In One
    HowtoexitNode.jsREPLenvironmentAllInOneexitNode.jsREPLmethods#doublepress✅$Ctrl+C#or✅$Ctrl+C$Ctrl+D#or✅$.exitexit(......