Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to scrape Comment using Beautifulsoup in Python?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Comments are provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. The Comment object is just a special type of NavigableString and is used to make the codebase more readable.

Syntax: 

<!-- COMMENT --> 

Below given examples explain the concept of comments in Beautiful Soup. 
Example 1: In this example, we are going to create a comment.

Python3




# Import Beautiful Soup
from bs4 import BeautifulSoup
  
# Create the document
doc = "<b><!-- COMMENT --></b>"
  
# Initialize the object with the document
soup = BeautifulSoup(doc, "html.parser")
  
# Get the whole comment inside b tag
comment = soup.b
  
# Print the comment
print(comment)

Output: 

<b><!-- COMMENT --></b>

Example 2: In this example, we are going to create a comment and see its type.

Python3




# Import Beautiful Soup
from bs4 import BeautifulSoup
  
# Create the document
doc = "<b><!-- COMMENT --></b>"
  
# Initialize the object with the document
soup = BeautifulSoup(doc, "html.parser")
  
# Get the whole comment inside b tag
comment = soup.b.string
  
# Print the type of the comment
print(type(comment))

Output: 

<class 'bs4.element.Comment'> 
My Personal Notes arrow_drop_up
Last Updated : 29 Dec, 2020
Like Article
Save Article
Similar Reads
Related Tutorials