When you're working with HTML in Beautiful Soup,
you can also use Beautiful Soup to modify your HTML parse tree.
In this demo, we'll see how.
We start off here in a brand-new Jupyter notebook.
From bs4 import BeautifulSoup, NavigableString, and Comment.
These are new elements that we'll add to an existing parse tree.
Here is what our original HTML page looks like.
It's a very simple HTML page.
It has a header, a body, paragraph elements, but no links or images.
It has a comment though.
Here is what this HTML page looks like when opened up within a browser.
Switch back to our Jupyter notebook here,
and let's read in the contents of this HTML file and create a soup object.
Let's take a look at the contents of this HTML page
by invoking the prettify function.
This is the HTML page that we're going to modify using Beautiful Soup.
We've already seen that we can navigate to specific tags
within our HTML by using the .property.
.title will give us access to the title tag within our HTML.
The name of the title tag is, of course, a title.
I'm now going to modify the tag's name and set it to be special_title.
If you take a look at the title tag within your Jupyter notebook,
you'll find that it now has the new tag name.
In fact,
this new updated tag name for your title tag is
reflected in the soup object as a whole.
Soup.prettify will show you that the title tag is now special_title.
Let's make some more modifications to our HTML content.
I'm going to find that p_tag which has the class apple applied.
This is the first p_tag that matches the search that we performed.
The class value is equal to apple.
I'm going to update the value associated with the
class attribute and set it to newton.
Once you've made this update,
if you take a look at the class attribute for this p_tag,
it's now equal to newton,
and this change is reflected in the soup object as well.
Soup.prettify will show you that Newton's Apple now
has the class newton applied.
Let's access another p_tag here using the find function,
that p_tag which has the class steve applied.
This is Steve Job's Apple.
I can use the del keyword in Python to delete the class
attribute associated with this p_tag.
This p_tag no longer has the class attribute applied.
And, as you might expect, this change is reflected in the entire soup object.
Soup.prettify shows us that Steve Job's Apple no longer has a class attribute.
Now you might have noticed that Steve Job's Apple doesn't
really have the right apostrophe for Jobs.
I'm going to change the string associated with this p_tag.
I set the string to be Steve Jobs' Apple,
with the apostrophe in the right place.
This change will be reflected in the string property in the p_tag as a whole,
as well as in the soup object as a whole.
Let's see some other ways we can modify the parse tree.
Find that p_tag which has the id = '3apples'.
This is a p_tag which says Not really an insignificant fruit.
If you want to keep the existing content of this p_tag,
but simply add a little bit, you can use the append function on your html_tag.
Let's go ahead and append Quite interesting.
And this will now be available at the end of your existing content.
The append function works kind of like a Python list append.
Similarly,
you can use the extend tag as well to extend your current
tag's contents with multiple elements.
Extend takes in a list with multiple elements,
and all of these elements will be added to the very end of your tag's contents.
I'm now going to add a NavigableString to our existing parse tree.
Access the h2_tag within our Beautiful Soup object.
This is the header 2 within our HTML.
I'm going to instantiate a new NavigableString
object that says about these apples.
I'll invoke the append function on the h2_tag,
and this time, I'm going to append a NavigableString.
This NavigableString will now become part of my HTML.
A NavigableString is what Beautiful Soup uses to represent
the string contents of your HTML element,
except with navigation properties.
Let's continue modifying our HTML by adding in a new comment.
Instantiate a comment object which says Interesting.
You can now append this comment to any tag of your choice.
I've appended it to the h2_tag,
and observe that this comment is now present as an HTML comment within h2.
All of the changes we've been making to our HTML so far
are changes made to the soup object.
Observe all of the changes available within the h2_tag,
including the comment that we just added.
The soup also reflects all of the other modifications that we've made.
You can also modify your HTML by adding new HTML elements.
I'm going to use the soup.new_tag function to create a new
anchor tag with this particular reference.
I set the string property of the anchor tag to be equal to Apple,
and I'm going to append this new tag to my h2_tag element.
This anchor tag will now be the child of this h2_tag.
Take a look at the h2_tag.
The a_tag is now its child.
Printing out our prettified soup will show you that the
changes are reflected in the soup object.
Here is a new anchor tag with a reference to the Apple Store.
Once you've made all of the modifications that you wanted to to the soup object,
you can write out the soup's contents to a new file,
ThreeApples_Updated.html f.write ( soup.prettify ).
If you now run an ls command within your files folder,
you'll find that this new file is present under there.
And if you run a cat command on this HTML file,
you'll find that it contains our modified tree.
All of the changes that we made in this program are reflected within this HTML.