Issue
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!'
Solution
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!'
Answered By - Przemek Baj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.