我正在使用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