【发布时间】:2012-04-18 14:10:51
【问题描述】:
我正在使用 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里面的代码crash,报错
UnicodeDecodeError at /
'utf8' codec can't decode byte 0x92 in position 85: invalid start byte
无法编码/解码的字符串是:不应该
我尝试过使用很多技术,包括 .encode('utf8'),但没有一个能解决问题。我有两个问题:
- 如何解决这个问题
- 当问题代码在尝试之间时,我的应用程序如何崩溃,除了
【问题讨论】:
-
发布整个回溯,而不仅仅是最后一行。
-
@Marcin 答案的快照。检查网页的编码对于调试编码错误非常有用。 [![在此处输入图片描述][1]][1] [1]:i.stack.imgur.com/jVHTy.png
标签: python web-scraping lxml