Scrapy编码数据错误
Scrapy encoding data wrong
出于某种原因,当我使用 Scrapy 从元素中获取文本值时,它显示正确,但是当我将它放入数组时,它编码不正确。
这里是测试:我用了Château
这个词。在一种情况下 test,scrapy 获取单词然后打印并将其添加到数组中。在第二种情况 test2 中,我直接复制粘贴从另一个测试中打印的单词到数组中。
这是我的 Scrapy python 脚本:
value=node.xpath('//AddrDisplayMemberSerialization/text()').extract_first()
print value;
array={'test':value,'test2':'Château'}
print array
数组自动对值进行编码。 python 是自动执行此操作还是 Scrapy 执行此操作?
为什么它们的编码方式不同?
这就是它在终端中的显示方式。
但是如果你想让它以 utf-8 格式显示,只需在 settings.py
中执行此操作
FEED_EXPORT_ENCODING = 'utf-8'
问题的发生是因为 Python2 和 Python3 之间的差异。如果您在 Python3 中执行此操作,它将立即生效
Python 3.6.2 (default, Jul 17 2017, 16:44:45)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> value = 'Château'
>>> print (value)
Château
>>> array={'test':value,'test2':'Château'}
>>> print(array)
{'test': 'Château', 'test2': 'Château'}
>>>
现在让我们回到Python2
Python 2.7.13 (default, Jul 18 2017, 09:17:00)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> value = 'Château'
>>> print value;
Château
>>> array={'test':value,'test2':'Château'}
>>> print array
{'test': 'Ch\xc3\xa2teau', 'test2': 'Ch\xc3\xa2teau'}
发生这种情况是因为当您打印数组时,它正在转换为字符串表示形式而不是 python
中的 unicode
>>> str(array)
"{'test': 'Ch\xc3\xa2teau', 'test2': 'Ch\xc3\xa2teau'}"
>>> print str(array)
{'test': 'Ch\xc3\xa2teau', 'test2': 'Ch\xc3\xa2teau'}
打印时你想做的是 unicode 转义
>>> print str(array).decode("unicode-escape")
{'test': 'Château', 'test2': 'Château'}
但是等等,这把打印弄乱了?那是因为打印这些字符所需的编码。拉丁语简称
>>> print str(array).decode("unicode-escape").encode("latin-1")
{'test': 'Château', 'test2': 'Château'}
只需升级到 python3,您的问题就会得到解决。但是您需要将打印语句更改为 print(...)
。或者使用我展示的代码来训练编码
出于某种原因,当我使用 Scrapy 从元素中获取文本值时,它显示正确,但是当我将它放入数组时,它编码不正确。
这里是测试:我用了Château
这个词。在一种情况下 test,scrapy 获取单词然后打印并将其添加到数组中。在第二种情况 test2 中,我直接复制粘贴从另一个测试中打印的单词到数组中。
这是我的 Scrapy python 脚本:
value=node.xpath('//AddrDisplayMemberSerialization/text()').extract_first()
print value;
array={'test':value,'test2':'Château'}
print array
数组自动对值进行编码。 python 是自动执行此操作还是 Scrapy 执行此操作?
为什么它们的编码方式不同?
这就是它在终端中的显示方式。
但是如果你想让它以 utf-8 格式显示,只需在 settings.py
FEED_EXPORT_ENCODING = 'utf-8'
问题的发生是因为 Python2 和 Python3 之间的差异。如果您在 Python3 中执行此操作,它将立即生效
Python 3.6.2 (default, Jul 17 2017, 16:44:45)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> value = 'Château'
>>> print (value)
Château
>>> array={'test':value,'test2':'Château'}
>>> print(array)
{'test': 'Château', 'test2': 'Château'}
>>>
现在让我们回到Python2
Python 2.7.13 (default, Jul 18 2017, 09:17:00)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> value = 'Château'
>>> print value;
Château
>>> array={'test':value,'test2':'Château'}
>>> print array
{'test': 'Ch\xc3\xa2teau', 'test2': 'Ch\xc3\xa2teau'}
发生这种情况是因为当您打印数组时,它正在转换为字符串表示形式而不是 python
中的 unicode>>> str(array)
"{'test': 'Ch\xc3\xa2teau', 'test2': 'Ch\xc3\xa2teau'}"
>>> print str(array)
{'test': 'Ch\xc3\xa2teau', 'test2': 'Ch\xc3\xa2teau'}
打印时你想做的是 unicode 转义
>>> print str(array).decode("unicode-escape")
{'test': 'Château', 'test2': 'Château'}
但是等等,这把打印弄乱了?那是因为打印这些字符所需的编码。拉丁语简称
>>> print str(array).decode("unicode-escape").encode("latin-1")
{'test': 'Château', 'test2': 'Château'}
只需升级到 python3,您的问题就会得到解决。但是您需要将打印语句更改为 print(...)
。或者使用我展示的代码来训练编码