The HTML parse tree that Beautiful Soup builds up
also allows us to navigate sideways,
and that's exactly what we'll see here in this demo.
Set up the import statements that you need for the program,
just webbrowser and BeautifulSoup is enough,
and let's take a look at the very simple HTML document
that we are going to navigate sideways.
I've purposefully set up this HTML with no spaces and indentation so
that when we're navigating this HTML tree sideways,
we don't encounter new lines.
Let's take a look at how this HTML page looks within a browser.
It's a very simple text page.
Let's switch over to our Jupyter notebook.
I'm going to open up this HTML page on my local machine with the open
command in Python and construct a BeautifulSoup object.
Here is the very simple non-prettified HTML that we are working with.
Let's view this with the right indentation by
invoking the soup.prettify function.
At the very top, we have the html tag.
Under that we have a body tag.
The body tag contains a single child with the tag a,
and this a tag contains three child elements.
Let's take a look at the contents of this a tag by
invoking the prettify function on soup.a.
Soup.a contains three child elements at the same level,
the b tag, the i tag, and the c tag.
All these three elements can be considered to be siblings.
Beautiful Soup allows you to navigate your HTML parse tree
sideways using the next_sibling property on any tag.
The a tag contains no sibling, it's an only child,
which is why next_sibling returns None.
Let's access the b element within our soup object.
The b tag here contains the text Tom and has siblings.
Let's use the next_sibling property to access the next sibling in the sequence,
and that is the i element.
Siblings in Beautiful Soup are elements which are at
the same level of the parse tree.
Let's take a look at the next sibling of the next sibling for the b tag,
and that gives you the c tag iconic pair.
Every tag contains the next_sibling property,
which allows you to move sideways at the same level.
Let's access the c tag that is present within our HTML.
Soup.c will give us iconic pair.
Observe that the next_sibling property of the c tag returns None.
There are no siblings after the c tag in sequence.
Let's go back and access the b tag on our soup object,
which contains the text Tom.
This Tom element has more than one sibling,
and you can access all of the siblings using an iterator by
accessing the next_siblings property of this b tag.
The next_siblings property returns a generator object,
and you can then use a for loop to iterate over this generator object.
The next siblings of the b tag are the i tag and the c tag.
Let's now work with the c tag present within our very simple HTML.
We know that the c tag does not contain any next_siblings.
This is the content iconic pair.
The c tag contains siblings before it though,
and you can access the previous sibling using the previous_sibling property.
The previous sibling for the c tag is the i tag with the content Jerry.
Every html tag has a previous_sibling property.
You can access the previous_sibling.previous_sibling property to move
sideways in the reverse direction through your HTML parse tree.
Now, of course, if you keep accessing the previous_sibling property,
at some point you'll come to a sibling which has no previous sibling,
and the HTML parse tree will return None.
For any HTML element that has multiple previous siblings,
you can access them using the previous_siblings property.
Notice the plural.
You can iterate over them using the previous_siblings generator object.