python 中的问题 withscrapy.response.follow ()

problem withscrapy.response.follow () in python

我是 scrapy 的新手,有点小问题。这是我的代码:

import scrapy


class SubcategoriasSpider(scrapy.Spider):
    name = 'subCategorias'
    start_urls = ['https://donvino.com.ar/categoria-producto/bodegas']

    def parse(self, response):
        bodegas = response.css('ul.menu#menu-bodegas li')
        for bodega in bodegas:
            url = bodega.css("a::attr(href)").get()
            #name_bodega = bodega.css('a::text').get() 
            yield response.follow(url,callback=self.parse_items)

    def parse_items(self,response):
        vinos = response.css('a.woocommerce-LoopProduct-link.woocommerce-loop-product__link')#no se puede dejar espacio vacío entre palabras, hay que poner un punto
        
        for vino in vinos:      

            yield {
              
                'nombre' : vino.css('h2::text').get(),
                'precio' : str(vino.css('span.woocommerce-Price-amount.amount::text').get()).replace(",", "")

            }

我需要在 def parse_items() 中添加 name_bodega = bodega.css('a::text').get()...像这样:

yield {
                'name_bodega' :  ............
                'nombre' : vino.css('h2::text').get(),
                'precio' : str(vino.css('span.woocommerce-Price-amount.amount::text').get()).replace(",", "")

            }

问题是 name_bodega 不在 vino 中……所以……我不知道如何解决……有什么办法可以添加酒窖这个名字吗??。 提前致谢!!!

您可以使用 meta 在回调函数之间传递项目

class SubcategoriasSpider(scrapy.Spider):
    name = 'subCategorias'
    start_urls = ['https://donvino.com.ar/categoria-producto/bodegas']

    def parse(self, response):
        bodegas = response.css('ul.menu#menu-bodegas li')
        for bodega in bodegas:
            url = bodega.css("a::attr(href)").get()
            name_bodega = bodega.css('a::text').get() 
            yield response.follow(url,callback=self.parse_items,meta={'item':{"name_bodegae": name_bodega}})

    def parse_items(self,response):
        vinos = response.css('a.woocommerce-LoopProduct-link.woocommerce-loop-product__link')#no se puede dejar espacio vacío entre palabras, hay que poner un punto
        
        for vino in vinos:      

            yield {
                'name_bodega':response.meta.get("item")['name_bodegae'], # or -> response.meta.get("item")["name_bodegae"]
                'nombre' : vino.css('h2::text').get(),
                'precio' : str(vino.css('span.woocommerce-Price-amount.amount::text').get()).replace(",", "")

            }

在此处查看详细信息