这是一个非常奇怪的错误,让我们看一下细节:
ts.py文件:
#-*- coding: utf-8 -*-
import requests
from lxml import html
headers = {
'Host':'www.baidu.com',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36',
}
def get_html(url,enable_proxy=None):
r = requests.get(url,headers=headers)
parser = html.HTMLParser(encoding='utf-8')
return html.document_fromstring(r.text, parser=parser)
p = get_html('http://www.baidu.com')
print p.xpath(u'//*[@id="setf"]/text()')[0].encode('utf-8')
如果我只是执行ts.py,则lxml完美无缺。
但是!当我将get_html
放入另一个文件时出现错误,请参阅以下内容:
ts.py:
#-*- coding: utf-8 -*-
import requests
from util import get_html
p = get_html('http://www.baidu.com')
print p.xpath(u'//*[@id="setf"]/text()')[0].encode('utf-8')
util.py:
#-*- coding: utf-8 -*-
import requests
from lxml import html
headers = {
'Host':'www.baidu.com',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36',
}
def get_html(url, enable_proxy=None):
r = requests.get(url,headers=headers)
parser = html.HTMLParser(encoding='uft-8')
return html.document_fromstring(r.text, parser=parser)
tun ts.py输出:
Traceback (most recent call last):
File "C:\Users\mithril\Desktop\1\ts.py", line 8, in <module>
p = get_html('http://www.baidu.com')
File "C:\Users\mithril\Desktop\1\util.py", line 15, in get_html
parser = html.HTMLParser(encoding='uft-8')
File "E:\Python27\lib\site-packages\lxml\html\__init__.py", line 1662, in __init__
super(HTMLParser, self).__init__(**kwargs)
File "parser.pxi", line 1597, in lxml.etree.HTMLParser.__init__ (src\lxml\lxml.etree.c:99825)
File "parser.pxi", line 792, in lxml.etree._BaseParser.__init__ (src\lxml\lxml.etree.c:92549)
LookupError: unknown encoding: 'uft-8'
我的环境:
我测试python 2.7 32位和64位两者,结果相同。
答案 0 :(得分:9)
有拼写错误:utf-8
不是uft-8
:
#parser = html.HTMLParser(encoding='uft-8')
parser = html.HTMLParser(encoding='utf-8')