首页新闻找找看学习计划

萌新用scrapy爬取豆瓣音乐,报错KeyError: 'item',求大神讲解。

0
[待解决问题]
class spider(scrapy.Spider):
    name='doubanmusic'
    start_urls=['https://m.douban.com/music/']
    allowed_domains=['music.douban.com']

    def parse(self,response):
        for types in response.xpath('//ul[@class="type-list"]/li/a'):
            item=DoubanmusicItem()
            type=types.xpath('text()').extract()[0]
            item['type']=type
            type_href=types.xpath('@href').extract()[0]
            item['type_url']=response.urljoin(type_href)
            yield item
            yield scrapy.Request(url=item['type_url'],meta={'item':item},callback=self.get_info)




    def get_info(self,response):
        for sel in response.xpath('//div[@class="pl2"]'):
            item=response.meta['item']
            song=sel.xpath('a/text()').extract()[0].strip()
            item['song']=song
            info=sel.xpath('p[@class="pl"]/text()').extract()[0]
            item['info']=info
            yield item


            next_page = response.xpath('//span[@class="next"]/a/@href').extract()
            if next_page:
                next_url = response.urljoin(next_page[0])
                yield scrapy.Request(url=next_url,callback=self.get_info)
王小伍sky的主页 王小伍sky | 初学一级 | 园豆:8
提问于:2018-07-11 11:24
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册