1.进入setting中把ITEM_piplines文件注销去掉
2.在piplines中写好代码
1 # -*- coding: utf-8 -*-
2
3 # Define your item pipelines here
4 #
5 # Don't forget to add your pipeline to the ITEM_PIPELINES setting
6 # See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
7
8 import json
9
10
11 class ItcastPipeline(object):
12
13 # __init__方法是可选的,作为类的初始化方法
14 def __init__(self):
15 #创建一个文件
16 self.filename = open("teacher.json", "w")
17
18 # process_item的方法是必须写的,用来处理item数据的
19 def process_item(self, item, spider):
20 # 有中文不能用ascii
21 jsontext = json.dumps(dict(item), ensure_ascii=False)
22 self.filename.write(jsontext.encode("utf-8")) + "\n"
23 return item
24
25 # close_spider方法是可选的,结束时调用这个方法
26 def close_spider(self):
27 self.filename.close()
3.注意
在主文件中不用return, 用yield.
转载于:https://www.cnblogs.com/cuzz/p/7623751.html