代码之家  ›  专栏  ›  技术社区  ›  gongarek

Scrapy中附加页的下一页

  •  -1
  • gongarek  · 技术社区  · 7 年前

    为什么我不能转到parse\u next中的下一页并合并某个date do对象?

    def parse(self, response):
        item = TItem()
        ...
        link_www = lekarz.xpath('whatever/@href').extract_first()
        request = scrapy.Request(link_www, callback=self.parse_next)
        request.meta['item'] = item
        yield request
    
        next_page = response.css('whenever::attr(href)').extract_first()
        if next_page is not None:
            yield response.follow(next_page, callback=self.parse)
    
    attri = []
    
    def parse_next(self, response):
        item = response.meta['item']
        self.attri.append(xpath("whatever")).extract_first
    
        next_pager = response.css('whatever_too_xd').extract_first()
        if next_pager is not None:
            yield response.follow(next_pager, callback=self.parse_next)
        else:
            item['hehe'] = self.attri
            yield item
    

    输出:

    KeyError:“项目”

    为什么?

    1 回复  |  直到 7 年前
        1
  •  1
  •   stasdeep    7 年前

    您没有将项目传递给回调。为此,只需添加 meta 参数到 response.follow 调用:

    response.follow(next_page, callback=self.parse_next, meta={'item': item})