Beautiful Soup supports a variety of different operations
that can be used to modify your HTML tree.
And in this demo, we'll explore a few more.
We'll open up and work with the same HTML file that
we used in the previous demo, SimpleApple.html under our files folder.
Let's first access the first p_tag present within the soup object.
This is the p_tag that contains the three significant apples.
I'm going to access the first p_tag within this p_tag,
which gives us Eve's apple.
If you want to modify the contents of this tag,
we've seen how you can use the extend function.
Extend also works with simple strings.
We now have the content appended to the very end of this p_tag.
Let's now access the p_tag which has the class attribute Apple applied.
This is the p_tag which is Newton's apple.
If you want to insert content at a specific position within the string,
you can use the insert function.
Specify the index position where you want the string to be inserted.
I've specified position 0.
The new string is appended to the very beginning of my tag's contents.
I'm going to use the find function to find the p_tag for Steve Job's Apple.
It has the class steve applied.
Beautiful Soup tags also support the insert_before and insert_after functions.
Insert_before the current content The third apple is.
The new string is prepended to the existing string.
You can use the insert, insert_before,
insert_after functions with html_tags as well.
Let's create a new anchor tag with this href string.
The string is just see here.
I'm going to insert this new a_tag after the current p_tag.
This newly created a_tag will become a sibling of the existing p_tag.
Let's run soup.prettify, and if you take a look at the current state of the HTML,
you'll find that the a_tag is a sibling of the p_tag.
It comes after the p_tag.
Our current p_tag references Steve Job's Apple,
but the way the apostrophe is placed is not correct.
So I'm going to go ahead and clear the contents of this p_tag.
And now this p_tag has no content.
I'll assign a new value to the .string property of this p_tag and update
how Steve Jobs' is written with the correct apostrophe.
All of the changes that we make using the different functions that
Beautiful Soup offers will be reflected in the soup object,
which can then be written out to a new file if you want to.
Let's say you have a tag within your HTML.
For example, let's work with the h2_tag, which we access using soup.h2.
Now this h2_tag has a parent when it's present within your parse tree.
The parent of the h2_tag happens to be the body_tag.
The extract function in Beautiful Soup removes a tag
or a string from the parse tree.
H2_tag.extract will get rid of the h2_tag from the parse tree.
The tag is still present, but if you take a look at the parent of the tag,
the parent will return None.
You have a reference to this tag within your program,
but it's no longer part of your parse tree.
And if you print out the contents of your soup object, you'll confirm this.
Observe that the h2_tag is no longer present within the body.
Let's access another tag within our parse tree.
This is the i_tag.
Soup.i gives us this i_tag.
You can use the decompose function to completely get
rid of a tag and all of its contents.
Decompose will completely destroy the i_tag.
The tag is now None, and it's no longer part of the parse tree.
Tag.parent will return None.
Both the extract,
as well as the decompose functions allow you to remove tags from a parse tree.
Decompose, however, destroys the tag completely.
Here is some simple markup that I can add to a soup object using Beautiful Soup.
It contains an anchor tag, and within that an h1_tag.
I'll now assign this anchor tag to a variable called
a_tag so that it's easy for me to use.
I'm now going to create a new tag within the soup object.
This is an i_tag, and I assign also called Titan arum.
This is the string I assigned to the i_tag.
Beautiful Soup has a replace_with function,
which allows you to replace a tag with another tag.
I'm going to replace the h1_tag, with this new i_tag,
and you can see that the soup has been updated.
Let's take a look at another function to modify an existing tree.
Here is a soup object with just an i_tag.
So you can see that the soup contains the HTML body and the i_tag within it,
I'm going to create a new b_tag using the new_tag function.
I'm going to use the wrap function in Beautiful Soup to wrap the
contents of the existing i_tag with this b_tag.
Note that the i_tag continues to be within our soup object.
But within the i_tag, we have the new b_tag,
which wraps the old contents of the i_tag.
The wrap function allows you to insert tags with an element.
The unwrap function does the reverse.
Here is a simple markup, which we instantiate into a soup object,
we have an h1 element, and within that we have a b_tag.
Now I think that b_tag is unnecessary,
so I'm going to extract the h1_tag into a new variable.
And I'm going to get rid of this b_tag using the unwrap function.
H1_tag.b .unwrap will get rid of the b_tag.
The h1_tag no longer has the b_tag within it,
it has been unwrapped,
and all of these changes are reflected within the larger soup object as well.