保存html到数据库报错
Whisper Lv4

存取富文本,或者爬取到的网页带有html标签,需要在mysql中以longtext类型存入原生html的数据。
存入时报错

1
2
query = query % self._escape_args(args, conn)
TypeError: not enough arguments for format string

开始以为是标签的原因,格式化后还是报错,如下格式化

1
2
import html
html_escaped = html.escape(content)

取出返回前端时,使用html.escape(content)转换后返回。

如果上面的转换任然不生效,查看原文中是否有%引起的,百分号与Python的格式化语法冲突,需要将百分号处理一下再存。