Skip to main content
 首页 » 编程设计

Python 错误 : 'utf8' codec can't decode byte 0x92 in position 85: invalid start byte

2025年05月04日11grandyang

我正在使用 python2.7 和 lxml。我的代码如下

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 

无法编码/解码的字符串是:不应该是

我尝试过使用很多技术,包括 .encode('utf8'),但没有一个能解决问题。我有 2 个问题:

  1. 如何解决这个问题
  2. 当问题代码在 try except 之间时,我的应用程序如何崩溃

请您参考如下方法:

页面使用 charset=ISO-8859-1 提供。将其解码为 un​​icode。

[