Zai*_*han 1 python lxml web-scraping
我正在使用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 = ''
Run Code Online (Sandbox Code Playgroud)
代码在try中崩溃,给出错误
UnicodeDecodeError at /
'utf8' codec can't decode byte 0x92 in position 85: invalid start byte
Run Code Online (Sandbox Code Playgroud)
无法编码/解码的字符串是:ouldn t
我尝试过使用很多技术,包括.encode('utf8'),但没有一个能解决问题.我有2个问题: