我使用python2.7和lxml来获取页面。我一直收到以下错误。在
(<type 'exceptions.UnicodeEncodeError'>, UnicodeEncodeError('ascii', u'Approximate Dimensions: 4\xbd" x 4" x 7" (assembled)', 25, 26, 'ordinal not in range(128)'), <traceback object at 0x7f9198ac48c0>)
我试过以下方法:
^{pr2}$如何解决这些错误?我需要能够1)保存到一个文本文件,然后2)上传文本文件到MySQL。在
谢谢
尝试:
它是unicode,您希望将它(作为文本)存储到utf-8。在
页眉所说的页面用于编码的内容可能与实际情况不同。如果页面的实际编码不是utf-8,那么做正确的业务就有点麻烦了。在
首先,您应该查看从
el.text_content()
返回的文本x = el.text_content() print x
{3{3}还没有解码的意思。在
如果x是unicode(以“u”开头),则应将
unicode
转换为str
,并用适当的编码(如cp1252
或其他符号)对其进行解码chars = ''.join([chr(ord(x)) for x in el.text_content()]) /// It will change your dumb unicode to str result = chars.decode({try with different encoding until it doesn't throw an error}) /// now you decode str with proper format
相关问题 更多 >
编程相关推荐