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

BeautifulSoup追加HTML字符串转义问题及韩文编码保留求助

问题解决方法

首先明确:你遇到的标签转义问题不是编码导致的,而是BeautifulSoup的append()方法默认会把传入的字符串当作普通文本处理,自动转义HTML特殊字符。韩文显示的问题你已经通过指定encoding='UTF8'解决,无需额外调整编码设置。

解决标签转义的核心是告诉BeautifulSoup:你传入的是HTML代码,不是普通文本。以下是两种常用解决方法:

方法1:先解析HTML字符串为节点再插入

把要追加的HTML字符串先转换成BeautifulSoup节点对象,再插入到文档中:

from bs4 import BeautifulSoup

# 读取原有HTML文件(保持你原有的编码设置)
soup = BeautifulSoup(open("test/test.html", 'rt', encoding='UTF8'), 'html.parser', from_encoding='UTF8')

# 将HTML字符串解析为可插入的标签节点
text = "<p>exampletext</p>"
new_node = BeautifulSoup(text, 'html.parser').p
soup.body.append(new_node)

# 保存并输出结果(编码设置不变)
with open("test/result.html", "w", encoding='UTF8') as file:
    file.write(str(soup))
print(str(soup))

方法2:使用replace_with注入HTML片段

创建临时文本节点,再替换为解析后的HTML内容:

from bs4 import BeautifulSoup, NavigableString

soup = BeautifulSoup(open("test/test.html", 'rt', encoding='UTF8'), 'html.parser', from_encoding='UTF8')

text = "<p>exampletext</p>"
# 创建临时文本节点,替换为解析后的HTML
temp_node = NavigableString(text)
temp_node.replace_with(BeautifulSoup(text, 'html.parser'))
soup.body.append(temp_node)

with open("test/result.html", "w", encoding='UTF8') as file:
    file.write(str(soup))
print(str(soup))

关于韩文显示的验证

只要你在读取和写入文件时都指定encoding='UTF8',韩文内容就会保持完整无乱码。上述方法仅处理HTML标签的转义逻辑,不会影响韩文的编码正确性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 07:14:58