lxmlを使用してWebページをダウンロードして解析し、UTF-8xml出力を作成する必要があります。擬似コードのスキーマはもっとわかりやすいと思います。
from lxml import etree
webfile = urllib2.urlopen(url)
root = etree.parse(webfile.read(), parser=etree.HTMLParser(recover=True))
txt = my_process_text(etree.tostring(root.xpath('/html/body'), encoding=utf8))
output = etree.Element("out")
output.text = txt
outputfile.write(etree.tostring(output, encoding=utf8))
したがって、webfileは任意のエンコーディングにすることができます(lxmlがこれを処理する必要があります)。出力ファイルはutf-8である必要があります。どこでエンコーディング/コーディングを使用するかわかりません。このスキーマは大丈夫ですか?(lxmlとエンコーディングに関する優れたチュートリアルは見つかりませんが、これには多くの問題があります...)堅牢なソリューションが必要です。
編集:
したがって、utf-8をlxmlに送信するには、
converted = UnicodeDammit(webfile, isHTML=True)
if not converted.unicode:
print "ERR. UnicodeDammit failed to detect encoding, tried [%s]", \
', '.join(converted.triedEncodings)
continue
webfile = converted.unicode.encode('utf-8')