Saturday, December 28, 2024
Google search engine
HomeLanguageschildren generator – Python Beautifulsoup

children generator – Python Beautifulsoup

children generator 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 children generator is used to iterate over the tag’s children. Each child is going to be the tag element.

Syntax: 

tag.children 

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

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 body tag
tag = soup.body
  
# Print the children of tag
for child in tag.children:
    print(child)


Output: 

<b> Hello world </b>
<body></body>

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

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 body tag
tag = soup.body
  
# Print the type of children of tag
for child in tag.children:
    print(type(child))


Output: 

<class 'bs4.element.Tag'>
<class 'bs4.element.Tag'>

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments