你如何让scrapy产生所有物品?

时间:2015-03-14 00:26:25

标签: python scrapy

我刚刚学会了scrapy。 你如何让scrapy产生所有物品?

例如,如果我想提取book。 主页是book title,第二层是chapter,第三层是article

class BookSpider(scrapy.spider.Spider):
   name = 'book'
   allowed_domains = ['book.com']
   start_urls = ['http://www.book.com']

   def __init__(self):
      self.items = []

   def parse(self, response):
      link = response.xpath('//chapter').extract()

      for l in links:
         yield Request(l, callback=self.parse_chapter)

      print self.items

   def parse_chapter(self, response):
      link = response.xpath('//article').extract()

      for l in links:
         yield Request(l, callback=self.parse_article)
      return

   def parse_article(self, response):
      item = BookItem()
      item['article'] = response.url
      self.items.append(item)
      return

但结果只是一个空列表。为什么self.items无法构建?

1 个答案:

答案 0 :(得分:1)

您需要从任何回调中返回或产生项目或项目列表:

def parse_article(self, response):
    item = BookItem()
    item['article'] = response.url
    return item