从解析方法到管道的价值

时间:2019-06-17 07:30:29

标签: python scrapy

我有以下管道和简单的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运行代码

0 个答案:

没有答案