如何防止美丽的小组向纯文本添加HTML标签
在我的应用程序中,我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
潜水后找到了解决方案。
我们要做的就是在创建BeautifutSoup实例:
输出:
After diving into BeautifulSoup docs, I've found a solution.
All we have to do is specify "html.parser" when creating BeautifulSoup instance:
output: