如何防止美丽的小组向纯文本添加HTML标签

发布于 2025-02-07 22:55:02 字数 467 浏览 1 评论 0原文

在我的应用程序中,我正在使用BeautifulSoup来处理所有电子邮件模板。

其中一些是完整的HTML文档,但其中一些只是纯文本(主要用于测试)。

正如问题标题中提到的那样,我想防止Beautifulsoup周围添加HTML标签。

这是最简单的示例:

soup1 = BeautifulSoup("Hello World!")
html1 = str(soup1)
print(html1)

当前输出:

'<html><head></head><body>Hello World!</body></html>'

预期输出:

'Hello World!'

In my app, I'm using BeautifulSoup to process all the email templates before they are sent.

Some of them are complete HTML docs, but some of them are just plain texts (mostly used in tests).

As it's mentioned in the question title, I want to prevent BeautifulSoup from adding HTML tags around the plain text.

Here is the simplest example:

soup1 = BeautifulSoup("Hello World!")
html1 = str(soup1)
print(html1)

current output:

'<html><head></head><body>Hello World!</body></html>'

expected output:

'Hello World!'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2025-02-14 22:55:02

潜水后找到了解决方案。
我们要做的就是在创建BeautifutSoup实例:

soup1 = BeautifulSoup("Hello World!","html.parser")
html1 = str(soup1)
print(html1)

输出:

'Hello World!'

After diving into BeautifulSoup docs, I've found a solution.
All we have to do is specify "html.parser" when creating BeautifulSoup instance:

soup1 = BeautifulSoup("Hello World!","html.parser")
html1 = str(soup1)
print(html1)

output:

'Hello World!'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文