items.py中定义如下:
class wileyItem(Item):
url = Field()
title = Field()
spider.py中反馈函数代码如下:
def parse(self, response):
sel = Selector(response)
base_url=get_base_url(response)
sites = sel.xpath(u'//ol[@id="issueTocGroups"]')
for site in sites:
item = wileyItem()
item['url']=site.xpath('//div').extract()
item['title']=site.xpath('//span').extract()
yield item
pipelines.py中是一段把item写入文本的json格式,代码如下:
def __init__(self):
self.file = open('items.jl', 'wb')
def process_item(self, item, spider):
line = json.dumps(dict(item)) + "\n"
self.file.write(line)
return item
我的问题:pipelines.py是在spider.py的for循环结束后激发运行的,还是在循坏中执行一条就激发pipelines.py一次?
多个item怎么保存到一个pipline中去?
如果你只是想确认这个问题,很简单。
你运行你的爬虫后,可以看:
shell里面的log
保存的文件是否一直在增加,db的数据是否一直在增加。(在爬的同时)
可以去看一下 python 协程
感觉类似这个http://python3-cookbook.readthedocs.io/zh_CN/latest/c04/p13_create_data_processing_pipelines.html
但是很不确定