Przemek Baj
Przemek Baj

Reputation: 494

How to prevent BeautifulSoup from adding HTML tags to the plain text

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!'

Upvotes: 1

Views: 214

Answers (1)

Przemek Baj
Przemek Baj

Reputation: 494

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!'

Upvotes: 1

Related Questions