Skip to content
Related Articles

Related Articles

Contents list – Python Beautifulsoup

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 25 Oct, 2020

The contents list is 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 content is a list that contains the tag’s children.

Syntax: 

 tag.contents 

Below given examples explain the concept of contents in Beautiful Soup. 
Example 1: In this example, we are going to get the contents of the element.

Python3




# Import Beautiful Soup
from bs4 import BeautifulSoup
  
# Create the document
doc = "<body><b> Hello world </b><body>"
  
# Initialize the object with the document
soup = BeautifulSoup(doc, "html.parser")
  
# Get the whole content from the body tag
contents = soup.body.contents
  
# Print the contents
print(contents)

Output: 

[<b> Hello world </b>, <body></body>]

Example 2: In this example, we are going to see the type of contents.

Python3




# Import Beautiful Soup
from bs4 import BeautifulSoup
  
# Create the document
doc = "<body><b> Hello world </b><body>"
  
# Initialize the object with the document
soup = BeautifulSoup(doc, "html.parser")
  
# Get the whole content from the body tag
contents = soup.body.contents
  
# Print the type of contents
print(type(contents))

Output: 

<type 'list'>


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!