首页 > 其他 > 详细

关于scrapy的piplines

时间:2017-10-03 15:18:35      阅读:652      评论:0      收藏:0      [点我收藏+]

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

技术分享

2.在piplines中写好代码

 1 # -*- coding: utf-8 -*-
 2 
 3 # Define your item pipelines here
 4 #
 5 # Dont 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.

关于scrapy的piplines

原文:http://www.cnblogs.com/cuzz/p/7623751.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!