Scrapy中关于Export Unicode字符集问题解决
来源:互联网 发布:北京淘宝城 编辑:程序博客网 时间:2024/06/05 03:11
使用命令行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 CustomJsonLinesItemExporterclass 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
2 0
- Scrapy中关于Export Unicode字符集问题解决
- 关于Unicode字符集编程
- 关于UNICODE字符集
- 关于Unicode字符集
- 关于Unicode字符集
- 关于Unicode字符集
- 关于Unicode字符集
- 9.关于Unicode字符集
- 关于VS2005 unicode字符集环境
- 学习笔记-关于字符集Unicode
- 关于Unicode,字符集,字符编码
- Visual Studio中C++关于Unicode字符集和多字节字符集
- Window中ASCII字符集,多字节字符集,UNICODE 字符集
- 关于字符集和Unicode的相关知识
- 关于字符集和Unicode的相关知识
- 关于字符、字符集、编码和Unicode
- 关于Unicode和ANSI字符集处理
- ECC6(Unicode)与其他系统接口字符集转换问题解决记录
- 学校学习ios教程笔记,第十节初识ios中的导航栏控制器页面跳转(UINavigationController)和模态
- spring 使用@scheduled注解执行定时任务
- dwr实现web单聊
- centos7 安装tcpdump 步骤
- 计算属性,样式和类绑定
- Scrapy中关于Export Unicode字符集问题解决
- 一天一条Linux指令-jobs
- session 入库
- VR系列——Oculus Audio sdk文档:一、虚拟现实音频技术简介(2)——定位和人耳听觉系统
- 4848. 【GDOI2017模拟11.3】永恒的契约
- JSP 如何获取下拉列表的选中的值是数据库表的对应数据
- 关于视图view
- php 日志、文件保存工具方法
- 简单爬虫入门:Python 爬虫之糗事百科