是的,代码缩进有问题。最后一行应该与前面的if语句对齐,而不是缩进一个额外的空格。正确的代码如下所示:
import scrapy
class BooksSpider(scrapy.Spider):
name = "books"
start_urls= ['http://books.toscrape.com/']
def parse(self, response):
for book in response.css('article.product_pod'):
name = book.xpath('./h3/a/@title').extract_first()
price = book.css('p.price_color::text').extract_first()
yield {
'name': name,
'price': price,
}
next_url = response.css('ul.pager li.next a::attr(href)').extract_first()
if next_url:
next_url = response.urljoin(next_url)
yield scrapy.Request(next_url, callback=self.parse)