BeautifulSoup: Comment Example

Syntax

Comment(Your_comment)

Comment example

from bs4 import BeautifulSoup, Comment

# Html source
html_source = '''
<div>
<h1>home 1</h1>
</div>
'''

# Parsing
soup = BeautifulSoup(html_source, 'html.parser')

# Find <div> 
el = soup.find('div')

# Create Comment
comment = Comment("Nice to see you.")
# Append Comment
el.insert(0, comment)

# Print
print(el)

Output:

<div><!--Nice to see you.-->
<h1>home 1</h1>
</div>