【问题标题】:Python, lxml and <type 'exceptions.UnicodeEncodeError'>Python、lxml 和 <type 'exceptions.UnicodeEncodeError'>
【发布时间】:2012-02-20 12:39:13
【问题描述】:

我正在使用lxml 从网页中获取字符串。我该怎么做才能获取我提取的数据字符串而不会出现以下错误?我想我只是无法使用str() 来解决问题。

在python中:

mystring = MySQLdb.escape_string(i.text_content())


(<type 'exceptions.UnicodeEncodeError'>, UnicodeEncodeError('ascii', u"\n\nEve Pownall\n\n  \n    \n    \n    \n        Eve Pownall\n\t  (Author)\n\t\n        \u203a Visit Amazon's Eve Pownall Page\n        Find all the books, read about the author, and more.\n\n         See search results for this author  \n        Are you an author?\n        Learn about Author Central\n        \n      \n   \n  \n\n  \n      amznJQ.onReady('bylinePopover', function () {});\n  \n\n\n (Author)\n\n\n\n\n\n\n\n\n\n\n", 75, 76, 'ordinal not in range(128)'), <traceback object at 0x7f225c99f050>)

【问题讨论】:

标签: python unicode lxml


【解决方案1】:

您需要以众所周知的编码(最有可能是 UTF-8)对字符串进行显式编码。

更多信息:

http://collective-docs.readthedocs.org/en/latest/troubleshooting/unicode.html

【讨论】:

  • 我讨厌 unicode。一直不明白。这是一个很好的链接,可以让事情变得干净如何处理它。
猜你喜欢
  • 2012-04-27
  • 1970-01-01
  • 2016-09-16
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
  • 2013-10-01
  • 2010-12-07
  • 1970-01-01
相关资源
最近更新 更多