chevron_left
Parent and Children Cookbook
0
0
0
new
Getting number of child elements of a tag in Beautiful Soup
Programming
chevron_rightPython
chevron_rightBeautiful Soup
chevron_rightCookbooks
chevron_rightParent and Children Cookbook
schedule Jul 1, 2022
Last updated Python●Beautiful Soup
Tags tocTable of Contents
expand_more To get the number of child elements of a tag, first fetch all the child elements of the tag using the find_all()
method, and then just check its length using len(~)
.
Example
Consider the following HTML document:
my_html = """ <p>Alex/p> <div> <p>Bob</p> <p>Cathy</p> </div>"""soup = BeautifulSoup(my_html)
To get the number of child elements of <p>Alex</p>
:
To get the number of child elements of <div>~</div>
:
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
Ask a question or leave a feedback...
0
0
0
Enjoy our search