Scrapy - Why Item Inside For Loop Has The Same Value While Accessed in Another Parser

后端 未结 1 1523
暖寄归人
暖寄归人 2021-01-28 00:38

I want to scrape the link inside the for loop, in for loop there are items, I passed the item to the callback function. But why the item in the callback function has the same va

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-28 00:47

    You should create item inside for loop, otherwise you just share same item between all the iterations repopulating its values only. So correct code is:

    def parse(self, response):
        for products in response.css("div.product-card"):
            item = Product()
            link = products.css("a::attr(href)").extract_first()
            item['sku'] = products.css("div.product-card::attr(data-sku)").extract_first()
            item['price'] = products.css("div.product-card__old-price::text").extract_first()
            yield item
    

    0 讨论(0)
提交回复
热议问题