使用python和lxml对页面进行爬网(<type'异常。UnicodeEncodeError'>,UnicodeEncodeError('ascii',

2024-06-16 10:39:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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。在

谢谢


Tags: intype错误asciinot页面lxmlexceptions
2条回答

尝试:

el.text_content().encode('utf-8')

它是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

相关问题 更多 >