In this demo, we'll get a quick overview of how encodings work in Beautiful Soup.
Your original document might have any encoding like ASCII or UTF-8,
but when you load that document into Beautiful Soup,
you'll discover that it's been converted to Unicode.
Here is some HTML markup specified using Unicode.
The snowman that you see within this markup is Unicode for a snowman.
Let's instantiate a soup object using this markup,
and let's extract the b_tag within the soup.
If you look at the contents of this b_tag, you'll see a little snowman in there.
That's what our Unicode did.
The document that was parsed into Beautiful Soup was interpreted as Unicode.
It so happened that our original markup was in Unicode in the first place.
Now let's encode this tag to be in the UTF-8 encoding.
This is what the byte representation of this b_tag looks like in UTF-8.
I'll assign my HTML code variable to this UTF-8-encoded tag,
and I'll instantiate a soup object using this HTML code.
If you take a look at soup.b, you'll find some strange characters in there.
This is because Beautiful Soup did not understand what the
original encoding of the document was.
Beautiful Soup tried to perform the best possible interpretation.
If you look at the original encoding property,
you'll see that Beautiful Soup thought it was of type Windows-1252.
When you know the original encoding of your document,
you can explicitly specify it using the from_encoding input argument.
The original encoding was UTF-8, and here is our snowman back again.
Thanks to our from_encoding input argument,
Beautiful Soup was able to identify the original encoding as UTF-8.
Let's encode the same b_tag into two different formats,
ISO-8859-7 and the ASCII format.
Both give me the same byte representation.
I'm now going to construct a little bit of HTML markup
using this encoded representation.
Observe that within the meta tag here,
I've specified that the charset is ISO-8859-7.
Now when you instantiate a soup object using Beautiful Soup,
it'll use this charset information from the meta tag.
When you print out soup.prettify, the snowman is back again.
The original encoding was identified, thanks to our meta tag,
and the output representation is by default in the UTF-8 format.