How to get the children of a tag in BeautifulSoup Python
In this tutorial, we are going to learn how to get the children of a tag in BeautifulSoup. We can find the children of any tag in two ways. By Knowing the class name and finding the elements of the class. contents()
and children()
are two methods by which we can find the children of the tag.
Importing the modules required
bs4 module:- From this module, we will use a library called BeautifulSoup for fetching the data from a webpage or XML Document, or HTML document. And also converting our data from string format to HTML format.
from bs4 import BeautifulSoup
Getting the children of a tag in BeautifulSoup
we will take an example HTML document named children.html.
HTML Document(children.html)
<my_class><h1>Welcome Home</h1><h2>How are you ?</h2><h3>Have a Nice day</h3></my_class>
By Using contents() method
We now use the contents method for getting all the children of a tag under a class.
from bs4 import BeautifulSoup with open("children.html","r") as con : #Storing our content in a variable content = con.read() # Parsering the html document soup = BeautifulSoup(str(content), 'html.parser') # Finding the requried tag Myclass = soup.find('my_class') # Print the children of the tag in a list print(Myclass.contents)
The output we get will be a list of all the tags of the class
Output
[<h1>Welcome Home</h1>, <h2>How are you ?</h2>, <h3>Have a Nice day</h3>]
To get the content of the tags ; Insert the code lines below
for each in Myclass: print(each.string)
We will obtain all the contents of the tags;
Output :-
Welcome Home How are you ? Have a Nice day
By Using children() method :-
In this method, we get our output as a generator object. So we iterate the output for getting the children of the tag.
from bs4 import BeautifulSoup with open("children.html","r") as con : #Storing our content in a variable content = con.read() # Parsering the html document soup = BeautifulSoup(str(content), 'html.parser') # Finding the requried tag Myclass = soup.find('my_class') # Print the children of the tag and it will be a generator object print(Myclass.children)
The generator object will be variable depending on the Python virtual machine(PVM). So the output be
Output:-
<list_iterator object at 0x00000240ED052DF0>
Now we will add the below code snippet to our code, Now we will get all the children of the given tag.
for each in Myclass.children: print(each.string)
And our final output will be as below;
Output:-
Welcome Home How are you ? Have a Nice day
Leave a Reply