我有以下管道和简单的Spider,是否有办法将file_name
从Spider的解析方法转换为管道?
import csv,datetime
class AaexampleSpider(object):
def __init__(self, file_name):
now = datetime.datetime.now()
self.current_date = now.strftime("%d%b")
self.file_name = file_name #file_name
self.infile = open("{}_{}.csv".format(self.current_date, self.file_name),"w")
self.dict_writer = csv.DictWriter(self.infile, fieldnames=["Hello"])
self.dict_writer.writeheader()
def process_item(self, item, spider):
self.dict_writer.writerow(item)
def close_spider(self, spider):
self.infile.close()
蜘蛛代码:
import scrapy
class ExampleSpider(scrapy.Spider):
name = 'example'
allowed_domains = ['example.com']
start_urls = ['http://example.com/']
def parse(self, response):
file_name = response.xpath('//div/h1/text()').extract_first()
#code
如果这不可能,我如何使用另一个蜘蛛将值传递给管道?我在终端中使用scrapy crawl example
运行代码