跳到内容
🛠️ToolsShed

HTML 编码器 / 解码器

将特殊字符编码为 HTML 实体,并将其解码回来。

关于此工具

HTML 编码将特殊字符转换为 HTML 实体引用——这些序列在网络文档中安全地表示字符。诸如 < (小于)、> (大于)、& (与号) 和 " (引号) 等常见字符在 HTML 标记中具有特殊含义,因此必须编码为 &lt;、&gt;、&amp; 和 &quot; 才能正确显示。解码会反转此过程,将实体转换回可读字符。这对网络开发人员、内容创建者以及处理代码片段、API 响应或可能包含 HTML 敏感字符的用户输入的任何人都至关重要。

要使用此工具,请将文本粘贴到输入字段中,然后选择编码(将特殊字符转换为实体)或解码(将实体转换回字符)。转换在浏览器中即时发生——不需要服务器上传。您可以对原始 HTML 进行编码以防止其被解释为标记,或对实体进行解码以查看原始文本。结果立即显示,可直接复制到剪贴板,使文档、论坛帖子、电子邮件撰写或数据准备任务的批量处理变得简单。

常见问题

代码实现

import html

# Encode: replace <, >, &, ", ' with HTML entities
raw = "<script>alert('XSS')</script> & <b>bold</b>"

encoded = html.escape(raw)
print(encoded)
# &lt;script&gt;alert(&#x27;XSS&#x27;)&lt;/script&gt; &amp; &lt;b&gt;bold&lt;/b&gt;

# Also encode quotes (useful inside HTML attributes)
encoded_all = html.escape(raw, quote=True)
print(encoded_all)
# &lt;script&gt;alert(&#x27;XSS&#x27;)&lt;/script&gt; &amp; &lt;b&gt;bold&lt;/b&gt;

# Decode: convert HTML entities back to plain text
decoded = html.unescape(encoded)
print(decoded)
# <script>alert('XSS')</script> & <b>bold</b>

# Safe injection into a template
user_input = "<img src=x onerror=alert(1)>"
safe_html = f"<p>{html.escape(user_input)}</p>"

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.