Scrapy中关于Export Unicode字符集问题解决
2016-11-03 15:47
204 查看
使用命令行
默认显示的中文是阅读性较差的Unicode字符,我们需要在settings文件中定义子类显示出原来的字符集。
之后使用命令行
Pipeline在Doc中的定义如下:
After an item has been scraped by a spider, it is sent to the Item Pipeline which processes it through several components that are executed sequentially
所以我们也可以在Pipeline类中直接定义输出:
scrapy crawl spider_name -o filename将制定内容的item信息输出时,scrapy使用默认的feed export对特定的file类型文件支持,例如json文件是
JsonLinesItemExporter,xml文件是
XmlItemExporter,有时候我们对export的形式或者内容不太满意时,可以自己继承上面的类,自定义export子类。
默认显示的中文是阅读性较差的Unicode字符,我们需要在settings文件中定义子类显示出原来的字符集。
from scrapy.exporters import JsonLinesItemExporter class CustomJsonLinesItemExporter(JsonLinesItemExporter): def __init__(self, file, **kwargs): super(CustomJsonLinesItemExporter, self).__init__(file, ensure_ascii=False, **kwargs) #这里只需要将超类的ensure_ascii属性设置为False即可 #同时要在setting文件中启用新的Exporter类 FEED_EXPORTERS = { 'json': 'porject.settings.CustomJsonLinesItemExporter', }
之后使用命令行
scrapy crawl spider_name -o filename.json就可以显示出正常可阅读的字符。
Pipeline在Doc中的定义如下:
After an item has been scraped by a spider, it is sent to the Item Pipeline which processes it through several components that are executed sequentially
所以我们也可以在Pipeline类中直接定义输出:
from project.settings import CustomJsonLinesItemExporter class DoubanPipeline(object): #定义开始爬虫的行为 def open_spider(self,spider): self.file=open('xxx.json','wb') self.exporter=CustomJsonLinesItemExporter(self.file) self.exporter.start_exporting() #定义爬虫结束的行为 def close_spider(self,spider): self.exporter.finish_exporting() self.file.close() #定义爬虫过程中的行为 def process_item(self, item, spider): self.exporter.export_item(item) return item
相关文章推荐
- 关于VC多字节字符集和unicode宽字符集的使用和问题,只有明白了其中的原理,才能灵活解决问题
- 关于VC多字节字符集和unicode宽字符集的使用和问题,只有明白了其中的原理,才能灵活解决问题
- (转)解决UNICODE字符集下CStdioFile的Writestring无法写入中文的问题
- 解决UNICODE字符集下CStdioFile的Writestring无法写入中文的问题
- 解决UNICODE字符集下CStdioFile的Writestring无法写入中文的问题
- 关于React Native 报Export declarations are not supported by current JavaScript version错误的解决问题
- 关于解决乱码问题的一点探索之二(涉及Unicode(utf-16)和GBK)
- Unicode字符集就是为了解决字符集这种不兼容的问题而产生的,它所有的字符都用两个字节表示,即英文字符也是用两个字节表示
- ECC6(Unicode)与其他系统接口字符集转换问题解决记录
- 关于Union 中 ORA-12704:字符集不匹配问题的解决
- Unicode字符集就是为了解决字符集这种不兼容的问题而产生的,它所有的字符都用两个字节表示,即英文字符也是用两个字节表示
- Unicode字符集就是为了解决字符集这种不兼容的问题而产生的,它所有的字符都用两个字节表示,即英文字符也是用两个字节表示
- 解决UNICODE字符集下CStdioFile的Writestring无法写入中文的问题和在在原文件后写入文件
- 关于Union 中 ORA-12704:字符集不匹配问题的解决
- C# 关于字符集的问题 默认 UTF-8 Unicode Default
- 关于CTime::Format在Unicode下的输出问题及解决办法
- 关于 VS2008 字符集改用多字节字符集时,控件显示样式变为旧样式的问题的解决
- 解决UNICODE字符集下CStdioFile的Writestring无法写入中文的问题
- 关于MFC使用 Unicode 字符集出现乱码的问题
- Unicode字符集就是为了解决字符集这种不兼容的问题而产生的,它所有的字符都用两个字节表示,即英文字符也是用两个字节表示