scrapy中文字符问题

问题描述

在scrapy spider的解析函数中,有时候通过如下两种方式获得的html数据中中文字符出现类似于\\u3010\\u6bdb\\u91cc这种格式的字符。

respone.text

或者

response.body.decode(response.encoding)

该字符串产生的问题是因为将unicode类型的数据转换为了str类型。在后续的处理中,中文字符只展示编码后的情况。

解决方法

html = response.body.decode(response.encoding)
html = html.encode().decode('unicode_escape')

通过上述方法,中文字符可以正常显示。

猜你喜欢

转载自blog.csdn.net/shuizhongmose/article/details/103425048