小编car*_*ron的帖子

python lxml模块在内部使用哪种编码?

当我得到一个网页时,我使用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)
Run Code Online (Sandbox Code Playgroud)

但是当我使用时:

text = doc.text_content()
print type(text)
Run Code Online (Sandbox Code Playgroud)

输出是<type 'lxml.etree._ElementUnicodeResult'>.为什么?我以为这将是一个utf-8字符串.

python encoding lxml

3
推荐指数
1
解决办法
4159
查看次数

标签 统计

encoding ×1

lxml ×1

python ×1