Scrapy(二)翻页功能

目录

方法一:利用框架内置函数

方法二:拼接URL

方法三:拼接简化版


Scrapy提供了很多种翻页的方式,记住其中常用的三种即可

下期会将一些CSS基本语法

这是spider.pyl里设置即可

方法一:利用框架内置函数

好处:可以从自己设置爬取的页面范围,即从第几页到第几页,在明确页码的情况下,可以用这种方法。

import scrapy
from my_scrapy.items import MyScrapyItem

class SpiderSpider(scrapy.Spider):
    # 爬虫名称
    name = 'spider'
    # 域名限制,允许爬取的范围
    # allowed_domains = ['https://quotes.toscrape.com/']
    base_url = 'https://quotes.toscrape.com/page/{}/'

    # 初始请求的页面
    start_urls = ['https://quotes.toscrape.com/']

    # 方法一:构造翻页函数
    def start_requests(self):
        for page in range(1,10):
            url = self.base_url.format(page)
            yield scrapy.Request(url,callback=self.parse)


    def parse(self, response):
        # text = response.text

        quotes = response.xpath('//div[@class="quote"]')
        for quote in quotes :
            # 旧方法 get()为新方法
            # text = quote.xpath('./span[@class = "text"]/text()').extract_first()

            # 实例化对象
            item = MyScrapyItem()

            # 利用xpth进行爬取
            text = quote.xpath('./span[@class = "text"]/text()').get()
            author = quote.xpath('.//small[@class="author"]/text()').get()
            Tags = quote.xpath('.//a[@class="tag"]/text()').getall()
            item['text'] = text
            item['author'] = author
            item['Tag'] = Tags

            # 迭代出去
            yield item

方法二:拼接URL

好处:可以自动爬取到最后一页,比较方便,不用取查看网页有几页。

import scrapy
from my_scrapy.items import MyScrapyItem


class SpiderSpider(scrapy.Spider):
    # 爬虫名称
    name = 'spider'
    # 域名限制,允许爬取的范围
    # allowed_domains = ['https://quotes.toscrape.com/']
    # base_url = 'https://quotes.toscrape.com/page/{}/'

    # 初始请求的页面
    start_urls = ['https://quotes.toscrape.com/']

    def parse(self, response):
        # text = response.text

        quotes = response.xpath('//div[@class="quote"]')
        for quote in quotes:
            # 旧方法 get()为新方法
            # text = quote.xpath('./span[@class = "text"]/text()').extract_first()

            # 实例化对象
            item = MyScrapyItem()

            # 利用xpth进行爬取
            text = quote.xpath('./span[@class = "text"]/text()').get()
            author = quote.xpath('.//small[@class="author"]/text()').get()
            Tags = quote.xpath('.//a[@class="tag"]/text()').getall()
            item['text'] = text
            item['author'] = author
            item['Tag'] = Tags

            # 迭代出去
            yield item
        
        # 拼接URL
        next = response.css('.pager .next a::attr("href")').get()
        url = response.urljoin(next)

        yield scrapy.Request(url=url, callback=self.parse)

方法三:拼接简化版

好处:代码简洁

import scrapy
from my_scrapy.items import MyScrapyItem


class SpiderSpider(scrapy.Spider):
    # 爬虫名称
    name = 'spider'
    # 域名限制,允许爬取的范围
    # allowed_domains = ['https://quotes.toscrape.com/']
    # base_url = 'https://quotes.toscrape.com/page/{}/'

    # 初始请求的页面
    start_urls = ['https://quotes.toscrape.com/']

    def parse(self, response):
        # text = response.text

        quotes = response.xpath('//div[@class="quote"]')
        for quote in quotes:
            # 旧方法 get()为新方法
            # text = quote.xpath('./span[@class = "text"]/text()').extract_first()

            # 实例化对象
            item = MyScrapyItem()

            # 利用xpth进行爬取
            text = quote.xpath('./span[@class = "text"]/text()').get()
            author = quote.xpath('.//small[@class="author"]/text()').get()
            Tags = quote.xpath('.//a[@class="tag"]/text()').getall()
            item['text'] = text
            item['author'] = author
            item['Tag'] = Tags

            # 迭代出去
            yield item


        # 简洁的拼接
        yield from response.follow_all(response.css('.pager .next a::attr("href")'), callback=self.parse)

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

依恋、阳光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值