When you use Beautiful Soup to search and filter the
contents of your HTML parse tree,
you'll mostly only work with the find and find_all functions.
But there are other functions available as well.
These offer slight modifications to the find and find_all functions.
They are not that commonly used though.
Here is the first a tag within our HTML page.
You can limit your search to just the parent elements of a
particular tag by using the find_parent function.
Here I want to find all parents of the a tag which have
the class property equal to container.
Find_parent will search up the parse tree from the a tag specified,
and there's exactly one element that matches,
the body tag.
Find_parent will find the first parent that matches your filter.
If you want to find multiple matches for the same filter,
you'll use the find_parents function.
Find_parents('p'), find all p tags which are parents of the a tag,
there's exactly one.
But if there were other parents that match the p tag,
those would be returned as well.
Let's perform another search and filter operation.
Here I want to find the next sibling of the a tag which
has the HREF attribute Href True.
The current a tag has a sibling anchor tag with the HREF property set.
This is what is returned.
If you want multiple siblings to be matched,
you'll use the find_next_siblings function.
HrefTrue will return multiple sibling matches.
Siblings refer to elements at the same level of the parse tree.
Let's find the next sibling with src True.
This will not return anything.
There are no image elements at the same level as this a tag.
Similarly, let's find_next¬_siblings with src True.
Once again this returns nothing.
It's an empty list.
However,
if you just want to search for elements which occur after the
a tag but need not be at the same level,
you can use the find_next function.
Find_next(src True),
will find any HTML element that matches this filter
down the parse tree after this a tag.
Find_next will return the first element that matches.
If you want to find all elements that match, you'll use find_all_next.
Find_all_next(src True) will return all image tags that match this property.
All of these image tags are after the a tag in our HTML.
Let's access another element here.
This is the image tag which has extra-info set to Tom_Cat.
I'm now going to use this image tag to perform some other
certain filter functions within Beautiful Soup.
I'll find the previous sibling, which has some value for the alt attribute,
altTrue.
Find_previous_sibling will find exactly the first
sibling that matches this filter, which is an image tag.
If you want to find multiple sibling matches before this element,
you'll use find_previous_siblings.
Notice the plural form of the function.
This will return all previous siblings where the
alt attribute has been specified.
Let's try and find a previous sibling for this image
tag which has the HREF attribute, Href True.
There are no previous siblings, nothing is returned.
If you want to find all previous siblings,
you need to use the plural form of this function,
and this returns an empty list.
Siblings just match elements at the same level of the parse tree.
If you want to match any element before this particular image tag,
you can use find_previous,
find a previous element before this image tag in our HTML parse tree,
which has the HREF attribute specified.
It will find the first a link navigating backwards from the current element.
If you want to find all elements before this particular image tag,
which has the HREF attribute specified, you'll use find_all_previous.
Find_all_previous will return all of the matched elements.
All of the functions that we studied here in this clip are
variations of the basic find and find_all functionality
available in Beautiful Soup.