To access the first, second, or N-th child div element in BeautifulSoup, use the .contents
or .find_all()
methods on a parent div element. The .contents
method returns a list of children, including tags and strings, while .find_all()
returns a list of matching tags only. Simply select the desired index to obtain the child div element you need.
In Beautiful Soup, you can navigate to the first, second, or third div
within a parent div
using the .contents
or .find_all()
methods.
Here’s an example:
from bs4 import BeautifulSoup html = """ <div id="parent-div"> <div class="child-div">First child div</div> <div class="child-div">Second child div</div> <div class="child-div">Third child div</div> </div> """ soup = BeautifulSoup(html, 'html.parser') # Find the parent div parent_div = soup.find('div', {'id': 'parent-div'}) # Method 1: Using .contents first_child_div = parent_div.contents[1] second_child_div = parent_div.contents[3] third_child_div = parent_div.contents[5] print("Using .contents:") print("First child div:", first_child_div.text) print("Second child div:", second_child_div.text) print("Third child div:", third_child_div.text) # Method 2: Using .find_all() all_child_divs = parent_div.find_all('div', {'class': 'child-div'}) print("\nUsing .find_all():") print("First child div:", all_child_divs[0].text) print("Second child div:", all_child_divs[1].text) print("Third child div:", all_child_divs[2].text)
The output of this script is:
Using .contents:
First child div: First child div
Second child div: Second child div
Third child div: Third child div
Using .find_all():
First child div: First child div
Second child div: Second child div
Third child div: Third child div
💡 Note:
The .contents
solution returns a list of the parent element’s children, including tags and strings. Note that the indexing numbers are shifted using this solution, i.e., the first element is indexed using .contents[1]
, the second with .content[3]
, and the n
-th with .contents[2*n-1]
.
The .find_all()
solution returns a list of matching tags only.
You can use either method to navigate to the first, second, or third div
within a parent div
.
Keep Learning
If you want to learn BeautifulSoup from scratch, I’d recommend you check out our academy course:


While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.