You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python的dominate库中插入不转义的HTML字符串?

解决Dominate库插入未转义HTML字符串的问题

Dominate库提供了raw()函数,专门用于插入无需转义的原生HTML内容,正好满足你类似JavaScript innerHTML的需求。

修改后的代码如下:

import dominate
from dominate.util import raw

hardcoded_html_string = 'Hello, <em>how are you doin?</em>'

doc = dominate.tags.html(title='doc')

with doc:
    dominate.tags.p(
        raw(hardcoded_html_string)
    )

print(doc)

执行后会得到预期输出:

<html title="doc">
  <p>Hello, <em>how are you doin?</em></p>
</html>

注意:使用raw()时要确保插入的HTML内容是可信的,避免引入XSS安全风险,切勿直接插入未经过滤的用户输入内容。

内容的提问来源于stack exchange,提问作者user171780

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 17:54:55