【发布时间】:2015-08-27 15:48:28
【问题描述】:
我似乎无法让程序识别 u'\xe9'(即“é”)。它似乎正在阅读 ascii 中的页面,这可能是问题所在。所以它不能正确打印“coupe”。任何想法如何解决这个问题?
from lxml import html
import requests
new_list = []
page=requests.get('http://www.carfolio.com/specifications/models/?man=557')
tree=html.fromstring(page.text)
model_name = tree.xpath('//span[@class="model name"]/text()'.encode('utf-8'))
for elem in model_name:
new_list.append(elem)
if u'\xe9' in elem:
u'\xe9'.encode('latin-1')
print(elem)
我以前从未处理过编码问题。我可以轻松地删除包含那个麻烦字节的元素,但那是删除我需要的数据。如果我切换编码,它会给我带来更奇怪的结果。
*python 3
【问题讨论】:
标签: python-3.x encoding decode url-encoding