You're saving an HTML fragment as UTF-8.
Normally you specify the character set for an HTML document by having a Content-Type, either in the HTTP headers, or in the HTML head. But you don't have HTTP (it's just a file), or an HTML head section (it's just a fragment), so there's no way to do either. So, your browser has to guess.
Most browsers in this case will default to Latin-1, although some will use your system character set instead, or offer a way to configure it, or will even try to magically guess. At any rate, if your browser tries to show UTF-8 as Latin-1, you'll end up with stuff like this:
Esta página ou secção foi marcada para revisão, …
Your browser probably has a way to override the default character set for a page. For example, with Safari, go to the View menu, then Text Encoding, then pick UTF-8, and:
Esta página ou secção foi marcada para revisão, …
So, how do you fix it permanently?
Well, you can't really fix it permanently, because there is no right way to store non-ASCII data in an HTML fragment. In fact, technically speaking, browsers shouldn't even be displaying HTML fragments like this as documents.
However, many browsers will let you toss in <meta>
tags at the very top of a fragment. So, it could be just a matter of this:
a.write('<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">')
a.write(document)
But that won't work with every browser, and isn't supposed to work; it just happens to with many of them.
What should be legal is to wrap the fragment in a document. This can be as simple as something like:
a.write('''<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"></head>
<body>{}</body>
</html>'''.format(document))
It's probably a better idea to figure out exactly which HTML version the page is written in, and use the appropriate doctype. But this should be good enough.