Webページを取得したら、次のようにUnicodeDammitを使用してutf-8エンコーディングに変換します。
import chardet
from lxml import html
content = urllib2.urlopen(url).read()
encoding = chardet.detect(content)['encoding']
if encoding != 'utf-8':
content = content.decode(encoding, 'replace').encode('utf-8')
doc = html.fromstring(content, base_url=url)
しかし、私が使用するとき:
text = doc.text_content()
print type(text)
出力は<type 'lxml.etree._ElementUnicodeResult'>
です。なぜ?utf-8文字列になると思いました。