您应该返回Request个实例,并在meta中传递项目.并且,您必须以线性方式使其建立并构建请求和回调链.为了实现它,您可以传递一个完成项目的请求列表,并从上一个回调中返回一个项目:
def parse_main_page(self, response):
item = MyItem()
item['main_url'] = response.url
url1 = response.xpath('//a[@class="link1"]/@href').extract()[0]
request1 = scrapy.Request(url1, callback=self.parse_page1)
url2 = response.xpath('//a[@class="link2"]/@href').extract()[0]
request2 = scrapy.Request(url2, callback=self.parse_page2)
url3 = response.xpath('//a[@class="link3"]/@href').extract()[0]
request3 = scrapy.Request(url3, callback=self.parse_page3)
request.meta['item'] = item
request.meta['requests'] = [request2, request3]
return request1
def parse_page1(self, response):
item = response.meta['item']
item['data1'] = response.xpath('//div[@class="data1"]/text()').extract()[0]
return request.meta['requests'].pop(0)
def parse_page2(self, response):
item = response.meta['item']
item['data2'] = response.xpath('//div[@class="data2"]/text()').extract()[0]
return request.meta['requests'].pop(0)
def parse_page3(self, response):
item = response.meta['item']
item['data3'] = response.xpath('//div[@class="data3"]/text()').extract()[0]
return item
另请参阅:
如果觉得《python子窗口返回数据给主窗口_Python Scrapy 从子页面返回进行抓取》对你有帮助,请点赞、收藏,并留下你的观点哦!