私は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 = ''
試行中にコードがクラッシュし、エラーが発生します
UnicodeDecodeError at /
'utf8' codec can't decode byte 0x92 in position 85: invalid start byte
エンコード/デコードできなかった文字列は次のとおりです。
.encode('utf8')を含む多くの手法を使用してみましたが、問題を解決できるものはありません。私は2つの質問があります:
- この問題を解決する方法
- 問題のコードが試行の合間にある場合、アプリがクラッシュする可能性はありますか?