gpt4 book ai didi

python - 将值从解析传递到管道初始化

转载 作者:太空宇宙 更新时间:2023-11-03 20:45:48 24 4
gpt4 key购买 nike

class AmazonSpider(scrapy.Spider):
name = 'Amazon'
start_urls = ['https://www.amazon.com/s?me=A39K8Q77DNOTN8&marketplaceID=ATVPDKIKX0DER']
def parse(self, response):
file_name = response.xpath('//title/text()').extract_first().replace(' @ Amazon.com: ','')
#code

def parse_more(self,response):
#code
yield item

管道.py

    def __init__(self):
now = datetime.datetime.now()
self.current_date = now.strftime("%d%b")
self.file_name = "test" #file_name
self.infile = open("{}_{}.csv".format(self.current_date,self.file_name),"w")
self.dict_writer = csv.DictWriter(self.infile)
self.dict_writer.writeheader()


def process_item(self, item, spider):
self.dict_writer.writerow(item)
#return item

如何将文件名从响应(解析中)传递到 __init__管道线(即 file_name 来自解析,我希望它是管道中的文件名)

最佳答案

在管道中,您必须让init读取arg,例如:

kwarg(非强制)

class AmazonfullPipeline(object):
def __init__(self,file_name=None):
self.file_name = file_name
...

arg(强制)

class AmazonfullPipeline(object):
def __init__(self,file_name):
self.file_name = file_name
...

例如:在其他文件中导入它

import filename.AmazonfullPipeline

class AmazonSpider(scrapy.Spider):
name = 'Amazon'
start_urls = ['https://www.amazon.com/s?me=A39K8Q77DNOTN8&marketplaceID=ATVPDKIKX0DER']
def parse(self, response):
file_name = response.xpath('//title/text()').extract_first().replace(' @ Amazon.com: ','')
do_something = AmazonfullPipeline(file_name)

关于python - 将值从解析传递到管道初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56614998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com