首頁  >  問答  >  主體

python - scrapy 再次请求的问题

如:item['url']=response.xpath('a/@href')分析出一个链接,
然后想从这个链接里的网页再获取一些元素,放入item['other']
应该怎么写,谢谢。

天蓬老师天蓬老师2720 天前743

全部回覆(1)我來回復

  • PHP中文网

    PHP中文网2017-04-18 10:30:58

    def parse_page1(self, response):
        for url in urls:
            item = MyItem()
            item['url'] = url
            request = scrapy.Request(url,callback=self.parse_page2)
            # request = scrapy.Request("http://www.example.com/some_page.html",dont_filter=True,callback=self.parse_page2)
            request.meta['item'] = item
            yield request
    
    def parse_page2(self, response):
        item = response.meta['item']
        item['other'] = response.xpath('/other')
        yield item

    最後附上官方文件https://doc.scrapy.org/en/lat...
    中文翻譯版http://scrapy-chs.readthedocs...

    回覆
    0
  • 取消回覆