我正在使用python2.7和lxml。我的代码如下Python错误:'utf8'编解码器无法解码位置85中的字节0x92:无效起始字节
import urllib
from lxml import html
def get_value(el):
return get_text(el, 'value') or el.text_content()
response = urllib.urlopen('http://www.edmunds.com/dealerships/Texas/Frisco/DavidMcDavidHondaofFrisco/fullsales-504210667.html').read()
dom = html.fromstring(response)
try:
description = get_value(dom.xpath("//div[@class='description item vcard']")[0].xpath(".//p[@class='sales-review-paragraph loose-spacing']")[0])
except IndexError, e:
description = ''
在try中的代码崩溃,给了一个错误
UnicodeDecodeError at/
'utf8' codec can't decode byte 0x92 in position 85: invalid start byte
不能被编码的串/解码为:ouldnt是
我有尝试使用许多技术,包括.encode('utf8'),但没有一个能解决问题。我有2个问题:
- 如何解决这个问题
- 如何将我的应用程序崩溃的问题时代码是一个尝试之间除了
发布整个回溯,而不仅仅是最后一行。 – Kylotan 2012-04-18 14:14:30
@ Marcin的答案快照。检查网页的编码对调试编码错误非常有用。 [1] [1] [在这里输入的形象描述] [1]!]:http://i.stack.imgur.com/jVHTy.png – 2016-01-19 23:27:35