We've seen that even very simple websites in the
real world contain a lot of HTML.
Now when you parse and extract specific bits of a website,
you might be interested in just a certain portion of the
HTML document and not the entire document,
which is why you may want to use something like
SoupStrainer to parse parts of a document.
SoupStrainer within Beautiful Soup allows you to parse just those
portions of a document that you're interested in,
allows you to choose which parts of the document you want to parse,
and limits parsing to just a subset of a document.
You can try SoupStrainer with the Mashable site that we saw earlier,
but to keep things simple here is some HTML code that
I set up in the form of a string.
The HTML string that we are working with is for this particular
page present in the TomJerry_WithImages.html file.
If you take a look at this web page here,
you can see that it contains a number of links and a number of images,
images of the Tom and Jerry cartoon, images of the creators of Tom and Jerry,
and an image of Tom and an image of Jerry.
Now assume that you're only interested in working with
the div tags within your HTML document.
Instead of parsing the entire document,
you could instantiate a SoupStrainer object which parses just the div tags.
Now when you instantiate a soup object using BeautifulSoup
and pass in the HTML contents of a page,
you can pass in the SoupStrainer that parses just the div tags
within your HTML using the parse_only argument.
Parse_only takes in a SoupStrainer object and will extract only those
contents of the document that you're interested in.
If you look at the soup object that you've just instantiated,
you'll see that it contains only the contents within
the div tags of your larger HTML.
HTML can be rather clunky and difficult to work with,
and the SoupStrainer helps mitigate this to a large extent.
Let's say you're only interested in extracting the
links that are present in the page.
You know that links are associated with a tags,
you can instantiate a SoupStrainer to search only for a tags within a page.
Once you have your SoupStrainer,
you can instantiate your BeautifulSoup object and pass in your
link_tags SoupStrainer into the parse_only input argument.
Our soup object now only needs to parse a subset of the original HTML,
just all of the a tags that exist within our original HTML code.
Limiting your parsing to just a subset of the original page can
also improve the performance of your parsing.
Here is another example of a SoupStrainer which I'm using to
extract just the img tags that exist within the page.
I pass in the img_tags,
and my soup now contains all of the imgs within the larger HTML.
The SoupStrainer works not only with tags but also with attributes.
Here I've instantiated a SoupStrainer to find all HTML elements
which have the alt tag set to creator_image.
The SoupStrainer filters by attribute and not by tag.
Let's pass in this SoupStrainer to our BeautifulSoup object,
and here is our resulting soup.
There are two images in our original HTML which have the
alt attribute set to creator_image, and both of these make up our soup.
Exactly like the find and findAll filter functions,
the SoupStrainer also works with regular expressions.
Here we want to extract all of the HTML elements
where the id contains the word link.
We'll use the SoupStrainer to parse only those elements with this kind of id,
and here are the four elements which match from our larger HTML.