首页 > 其他分享 >pipelines file

pipelines file

时间:2023-09-21 20:02:03浏览次数:41  
标签:pipelines self spider item json file def

1. 自定义实现文件存储:

(1). spidertest/pipelines.py:

import codecs
import json

class JsonPipeline(object):
   # 自定义json文件的导出
   def __init__(self):
      # 打开json文件
      self.file = codecs.open('test.json', 'w', encoding="utf-8")
   def process_item(self, item, spider):
      # ensure_ascii不设置false,写入中文或其它编码会出错
      lines = json.dumps(dict(item), ensure_ascii=False) + "\n"
      self.file.write(lines)
      return item
   # 当srapy关闭的时候,关闭文件
   def spider_closed(self, spider):
      self.file.close()

注:
①. codecs比open简化了编码的细节.

(2). spidertest/settings.py:

ITEM_PIPELINES = {
   'spidertest.pipelines.JsonPipeline': 2,
}

2. 使用scrapy自带的:

spidertest/pipelines.py:

from scrapy.exporters import JsonItemExporter

class JsonExporterPipleline(object):
   #调用scrapy提供的json export导出json文件
   def __init__(self):
      self.file = open('test.json', 'wb')       # wb是二进制的方式
      self.exporter = JsonItemExporter(self.file, encoding="utf-8", ensure_ascii=False)
      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

注:
①. exporters还提供了csv、xml写入.

标签:pipelines,self,spider,item,json,file,def
From: https://blog.51cto.com/u_16251183/7556518

相关文章

  • 必看!S3File Sink Connector 使用文档
    S3File是一个用于管理AmazonS3(SimpleStorageService)的Python模块。当前,ApacheSeaTunnel已经支持S3FileSinkConnector,为了更好地使用这个Connector,有必要看一下这篇使用文档指南。描述将数据输出到AWSS3文件系统。提示:如果您使用的是Spark/Flink,在使用此连接......
  • docker-dockerfile-docker镜像制作-基于本地模板创建镜像
    1.基于本地模板创建基于本地模板创建Docker镜像的步骤可以归纳如下:下载所需模板:首先,你需要在网络上找到你需要的Docker模板,并下载到本地。你可以从DockerHub或者其他的镜像仓库中获取到所需的模板。解压下载的模板:可以使用类似于7-Zip这样的工具来解压下载的模板文件。导入......
  • JAVA中常用IO流类:FileReader和FileWriter
    1,FileWriter类(字符输出流类)构造方法:FileWriterfw=newFileWriter(StringfileName);//创建字符输出流类对象和已存在的文件相关联。文件不存在的话,并创建。                                            如:FileWriterfw=new......
  • File文件流与base64图片格式转换
    //base64转文件exportfunctionbase64ToFile(base64:any,fileName:string){if(!base64)returnletdata=base64.split(',');lettype=data[0].match(/:(.*?);/)[1];constbstr=window.atob(data[1]);letn=bstr.length;constu8arr=......
  • 生成HFile以及入库到HBase
    一、MR生成HFile文件packageinsert.tools.hfile;importjava.io.IOException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.Path;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.KeyValue;importorg.a......
  • HBase 之HFileOutputFormat
     hadoopmr输出需要导入hbase的话最好先输出成HFile格式,再导入到HBase,因为HFile是HBase的内部存储格式,所以导入效率很高,下面是一个示例1.创建HBase表t11.hbase(main):157:0*create't1','f1'2.0row(s)in1.3280seconds3.4.hbase(main):158:0>5.ROW......
  • Physical file does not exist, SAS.ReportFontsforClients.9.4.saswcur.ttf
    ERROR:Physicalfiledoesnotexist,\SAS\ReportFontsforClients\9.4\saswcur.ttfAcircumventionforthisissueistoaddthefollowingOPTIONSstatementtoyourSAScodebeforetheODSPDFFILE=statement:optionssysprintfont="Courier";To......
  • Springboot 的 maven项目利用 maven插件构建 docker 镜像(免 DockerFile编写)
    Springboot的maven项目利用maven插件构建docker镜像(免DockerFile编写)本小节目的是springboot项目maven插件构建容器实验步骤:1.本地创建springboot项目,写业务逻辑代码2.提交代码到远程git仓库3.在linux环境拉取远程git仓库代码,构建镜像4.把构建完成的镜像推送......
  • 如何最简洁的使用iOS 开发证书 和 Profile 文件
    如果你想在iOS设备(iPhone/iPad/iTouch)上调试,需要有iOS开发证书和Profile文件。在你拿到这两个文件之后,该如何使用呢?证书使用说明:1. iOS开发证书:开发证书(DevelopmentCertificate)是一个后缀为.p12 的文件(Certificates.p12); 在Mac系统下,双击这个文件,这个证书会自动导......
  • 如何使用iOS 开发证书 和 Profile 文件
    如果你想在iOS设备(iPhone/iPad/iTouch)上调试,需要有iOS开发证书和Profile文件。在你拿到这两个文件之后,该如何使用呢?证书使用说明:1. iOS开发证书:开发证书(DevelopmentCertificate)是一个后缀为.p12 的文件(Certificates.p12); 在Mac系统下,双击这个文件,这个证书会自动导......