2025年3月14日 星期五 甲辰(龙)年 月十三 夜 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

scrapy框架pipelines管道文件基本写法

时间:12-11来源:作者:点击数:15
CDSY,CDSY.XYZ

scrapy框架pipelines管道文件基本写法

  • import json
  • # 转码操作,继承json.JSONEncoder的子类,在json目录下的encoder.py中
  • class MyEncoder(json.JSONEncoder):
  • def default(self, o):
  • if isinstance(o, bytes):
  • return str(o, encoding='utf-8')
  • return json.JSONEncoder.default(self, o)
  • class MySpiderPipeline(object):
  • def __init__(self):
  • self.filename = open("myInfo.json","w",encoding="utf-8")
  • def process_item(self, item, spider):
  • text = json.dumps((dict(item)),ensure_ascii=False,cls=MyEncoder) +'\n'
  • self.filename.write(text)
  • return item
  • def close_spider(self,spider):
  • print("数据处理完毕,谢谢使用!")
  • self.filename.close()
CDSY,CDSY.XYZ
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐