class DoubanzufangSpider(scrapy.Spider):
name = 'doubanzufang'
start_urls = ['https://www.douban.com/group/tianhezufang/discussion?start=50']
def parse(self, response):
item = ZufangItem()
node_list = response.css(".olt>tr>td.title")
for node in node_list:
item['title'] = node.css("a[href]::attr(title)").extract()[0]
item['url'] = node.css("a[href]::attr(href)").extract()[0]
yield scrapy.Request(item['url'], meta={'item': item}, callback=self.parse_detail)
def parse_detail(self, response):
item = response.meta['item']
item['poster'] = response.xpath(".//*[@id='content']/div/div[1]/div[1]/div[2]/h3/span[1]/a/text()").extract()[0]
item['image'] = response.xpath(".//*[@id='content']/div/div[1]/div[1]/div[1]/a/img/@src").extract()[0]
item['information'] = response.xpath(".//*[@id='link-report']/div[1]/p[1]/text()").extract()[0]
yield item
确认item['title'],item['url']都能获取到不止一个str
问题:
1:为什么我输出得到的item['title'],item['url']永远是最后一个?yield meta不应该每次都传递过去吗?
2:我想实现获取图1标题,作者以及图2内容,把他们保存在一个item返回,怎么实现?
图1:
图2:
解決了
问题1:应该是item保存的是字典,在for循环外声明的item每次在最后都把前面的更新了,所以url,title永远都是获取最后的值
问题2:将item = ZufangItem()放在for循环内即可解决