chevron_left
Parent and Children Cookbook
thumb_up
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings
Accessing an element's parent in Beautiful Soup
Programming
chevron_rightPython
chevron_rightBeautiful Soup
chevron_rightCookbooks
chevron_rightParent and Children Cookbook
schedule Jul 1, 2022
Last updated local_offer Python●Beautiful Soup
Tags tocTable of Contents
expand_more To access an element's parent in Beautiful Soup, use the Tag.parent
property.
Example
Consider the following HTML document:
my_html = """ <div id="names"> <p>Alex</p> <p>Bob</p> </div>"""soup = BeautifulSoup(my_html)
The parent property of elements
To get the parent of the first p
tag:
The parent property of strings
All NavigableString
objects, accessed via the tag's string
property, also possess the parent
property. The parent
in this context represents the tag that encloses the string.
For instance, the parent of Alex string
is:
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!