关于scrapy的piplines

时间:2023-05-16 19:01:02

1.进入setting中把ITEM_piplines文件注销去掉

关于scrapy的piplines

2.在piplines中写好代码

 # -*- coding: utf- -*-

 # Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html import json class ItcastPipeline(object): # __init__方法是可选的,作为类的初始化方法
def __init__(self):
#创建一个文件
self.filename = open("teacher.json", "w") # process_item的方法是必须写的,用来处理item数据的
def process_item(self, item, spider):
# 有中文不能用ascii
jsontext = json.dumps(dict(item), ensure_ascii=False)
self.filename.write(jsontext.encode("utf-8")) + "\n"
return item # close_spider方法是可选的,结束时调用这个方法
def close_spider(self):
self.filename.close()

3.注意

在主文件中不用return, 用yield.