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

关于scrapy的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.

相关文章:

  • 2021-10-28
  • 2021-12-22
  • 2021-08-23
  • 2021-09-16
  • 2022-12-23
  • 2022-12-23
  • 2021-12-13
  • 2021-06-19
猜你喜欢
  • 2022-12-23
  • 2021-09-11
  • 2021-12-06
  • 2021-12-21
  • 2021-07-31
  • 2022-12-23
  • 2021-12-31
相关资源
相似解决方案