If you're only going to work with certain parts of an HTML document,
parsing the entire document might be a waste of Python memory.
The SoupStrainer class within Beautiful Soup allows you to choose
which parts of an incoming document are parsed.
Let's assume that we have the entire HTML content that we
are interested in in the form of a string,
which I've assigned to the HTML code variable.
In order to work with just the div tags within this document,
you can instantiate a SoupStrainer object and specify that
you're interested only in the div_tags.
When you then create a soup object,
use the parse_only input argument and specify the
SoupStrainer that you just instantiated.
The resulting soup will contain only the div_tags within your larger HTML.
And really this is all there is to SoupStrainer.
Here is a SoupStrainer to work with just the a_tags in your document.
Parse the SoupStrainer object in using the parse_only
input argument to your Beautiful Soup,
and the resulting soup will contain only the a_tags.
The SoupStrainer can be specified using attributes as well.
Let's say you're interested only in those html_tags which
have the alt attribute set to creator_image.
Instantiate a SoupStrainer in this manner.
Instantiate a soup object by specifying this alt_attr as your SoupStrainer.
I use the default lxml parser.
There is one thing to note here.
The soup strainer does not work with the html5lib parser.
The way html5lib works under the hood,
it requires that the entire HTML document exists in memory.
And here is my resulting soup.
This contains only those tags which have an alt_attr set to creator_image.
Just like Beautiful Soup filters,
the SoupStrainer accepts regular expressions as well.
Here is a SoupStrainer for all tags where the ID contains the term link.
Parse this into your Beautiful Soup as you instantiate the object.
And here is what my soup looks like.
Here is a SoupStrainer that allows me to parse only those parts of
my HTML documents which have href attribute,
href = True.
Instantiate the soup object,
parse the strainer in in the parse_only input argument,
and here is the resulting soup.
The soup contains only those a_tags that have an href attribute.