你怎么能让羊痒交出所有的东西?

2024-04-23 11:50:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近刚学会了刮痧。 你怎么能让羊痒交出所有的东西?在

例如,如果我想提取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无法构建?在


Tags: inselfcomforparseresponsedefarticle