我究竟做错了什么 ?我想让蜘蛛使用URL爬到下一页

时间:2019-07-30 19:03:43

标签: scrapy

我是新手。我正在研究简单的教程。 一切正常,除了我无法爬到下一页。

import scrapy

class QuoteSpider(scrapy.Spider):
    name = "quotes"
    start_urls=['http://quotes.toscrape.com']
    allowed_domains = ["quotes.toscrape.com"]

    def parse(self,response):
        for response in response.xpath('//div[@class="quote"]'):
        yield { 
            "quote":response.xpath('./span[@class="text"]/text()').extract(),
            "author" : response.xpath('./span/small[@class="author"]/text()').extract(),
            "tag" : response.xpath('./div[@class="tags"]/a/text()').extract()
        }
        next_page = response.xpath('//nav/ul[@class="pager"]/li[@class="next"]/a/@href').extract_first()
        if next_page is not None:
            next_page_url = response.urljoin(next_page)
            yield scrapy.Request(url=next_page_url,callback=self.parse)

我的错误消息:

  

next_page_url = response.urljoin(next_page)

     

AttributeError:“选择器”对象没有属性“ urljoin”

1 个答案:

答案 0 :(得分:1)

问题是您正在用for循环覆盖响应对象。因此,for循环中的内部响应对象只是类型item = Split ( apples, "-")(0) ,其中不包含urljoin的定义。这应该可以解决您的问题。

spidy.language.path_node.PathNode