构造请求和无忧爬虫

如何实现翻页请求

百度贴吧翻页

requests模块是如何实现翻页的?

1.首先,获取当前页面的url地址
2.人为地点击下一页的按钮,获取第2页和第3页的url地址
3.通过比较这两个url地址的不同点,找出在翻页的时候,url地址的变化规律
4.找出规律之后,将这个规律用于第1页和第4页的url地址上,直接在浏览器中输入我们认为的第1页和第4页的url地址,观察页面的变化情况

实例:找出百度贴吧页面url地址的变化规律

第1页
第2页
第3页

https://tieba.baidu.com/f?kw=oldba1&ie=utf-8
https://tieba.baidu.com/f?kw=oldba1&ie=utf-8&pn=50
https://tieba.baidu.com/f?kw=oldba1&ie=utf-8&pn=100

规律其实已经很明显了
https://tieba.baidu.com/f?kw=oldba1&ie=utf-8&pn=*(页数-1)50

selenium模块是如何实现翻页的?

在这里插入图片描述

我们应该如何通过scrapy框架实现翻页请求?(思路)

1.找到下一页的地址

  • 1.1.获取:href属性中的内容
  • 1.2.构造:找规律

2.构造一个关于下一页url地址的requests请求传递给调度器

实现翻页请求

通过爬取51job中的招聘信息,学习如何实现翻页请求
https://jobs.51job.com/beijing/
在这里插入图片描述
在这里插入图片描述
下面是要爬取的数据(简单爬取,重点在于翻页部分)
在这里插入图片描述
只有5000页
在这里插入图片描述

https://jobs.51job.com/beijing/  第1页
https://jobs.51job.com/beijing/p2/  第2页
https://jobs.51job.com/beijing/p3/  第3页
https://jobs.51job.com/beijing/p4/  第4页
页面变化规律
https://jobs.51job.com/beijing/p页数/

第1页也符合要求
在这里插入图片描述

hr.py

# -*- coding: utf-8 -*-
import scrapy


class HrSpider(scrapy.Spider):
    name = 'hr'
    allowed_domains = ['51job.com']
    start_urls = ['https://jobs.51job.com/beijing/']

    def parse(self, response):
        div_list=response.xpath('//div[@class="detlist gbox"]//div')
        num=1   # 计数器
        for div in div_list:
            item={}
            item["title"]=div.xpath('.//span[@class="title"]/a/@title').extract_first()
            item["location_name"]=div.xpath('.//span[@class="location name"]/text()').extract_first()
            item["money"]=div.xpath('.//span[@class="location"]/text()').extract_first()
            print(num)
            num+=1
            yield item

        # 找到(或构造)下一页的url地址
        for page in range(2,5):
            next_url=f"https://jobs.51job.com/beijing/p{page}/"

            """将下一页的url地址传递给爬虫"""
            # 如果下一页数据的处理方式与第一页一样
            yield scrapy.Request(
                next_url,
                callback=self.parse
            )
            """
            scrapy.Requests()知识点:
            scrapy.Requests能构建一个requests,同时指定提取数据的callback函数
            scrapy.Requests(url, callback=None, method='GET', headers=None, body=None,
                 cookies=None, meta=None, encoding='utf-8', priority=0,
                 dont_filter=False, errback=None, flags=None, cb_kwargs=None)
            备注:
            1.我们可以根据自己的需要构建一个headers和cookies,但是注意不能够将cookies参数放置在headers中,因为scrapy.Requests()方法专门提供了一个位置用于存放cookies参数
            headers={"":"","",""},cookies=""
            2.scrapy.Requests()常用参数为:
            - callback:指定传入的url交给哪个解析函数去处理
            - meta:实现在不同的解析函数中传递数据;meta默认会携带部分信息,比如下载延迟,请求深度等;meta是字典的形式
                def parse1(self, response):
                    response.meta["item"]

                    yield scrapy.Request(
                        next_url,
                        callback=self.parse1,
                        meta={"item": item}
                    )
            - dont_filter:让scrapy的去重(请求过的url地址,scrapy是不会再请求的)不会过滤当前的url,scrapy默认有去重的功能,对需要重复请求的url有重要用途
            """

    # 如果下一页数据的处理方式与第一页不一样
    # 重新定义一种方法
    """
    def parse1(self, response):
        response.meta["item"]
       
        yield scrapy.Request(
                    next_url,
                    callback=self.parse1,
                    meta={"item":item}
                )
    """

pipelines.py

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


class WuyouPipeline:
    def process_item(self, item, spider):
        print(item)
        return item

cmd

scrapy startproject WuYou

cd WuYou

scrapy genspider hr 51job.com

scrapy crawl hr

settings.py

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

# -*- coding: utf-8 -*-

# Scrapy settings for WuYou project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://docs.scrapy.org/en/latest/topics/settings.html
#     https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://docs.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'WuYou'

SPIDER_MODULES = ['WuYou.spiders']
NEWSPIDER_MODULE = 'WuYou.spiders'

LOG_LEVEL="WARN"

# Crawl responsibly by identifying yourself (and your website) on the user-agent

# 设置"user-agent"(需要自定义)
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'

# Obey robots.txt rules
ROBOTSTXT_OBEY = True

# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
#COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
#}

# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    'WuYou.middlewares.WuyouSpiderMiddleware': 543,
#}

# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#    'WuYou.middlewares.WuyouDownloaderMiddleware': 543,
#}

# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
   'WuYou.pipelines.WuyouPipeline': 300,
}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

输出结果

1
{'title': '业务专员(黑龙江省佳木斯市)', 'location_name': '异地招聘', 'money': '6-9千/月'}
2
{'title': 'Premier Relationship Assistant  ID63983', 'location_name': '北京', 'money': None}
3
{'title': 'Android开发工程师(Flutter Developer0', 'location_name': '北京', 'money': '1.8-2.8万/月'}
4
{'title': '经营数据分析师', 'location_name': '北京', 'money': '1.5-4.5万/月'}
5
{'title': 'HRBP-研发方向', 'location_name': '北京-朝阳区', 'money': '2.5-3万/月'}
6
{'title': '保险理赔业务经理', 'location_name': '北京', 'money': '1-3万/月'}
7
{'title': '店铺实习生', 'location_name': '北京', 'money': '25元/小时'}
8
{'title': '医药代表-糖尿病产品(SGLT2)-北京', 'location_name': '北京-朝阳区', 'money': None}
9
{'title': '酒店业务BD(北京)', 'location_name': '北京', 'money': '0.7-1.3万/月'}
10
{'title': '销售代表', 'location_name': '北京-顺义区', 'money': '0.4-1万/月'}
11
{'title': 'Head of Service', 'location_name': '北京-朝阳区', 'money': None}
12
{'title': '(K12教育)营销策划师(J16446)', 'location_name': '北京-海淀区', 'money': '1-1.5万/月'}
13
{'title': '出国考试教师-ACT(J10429)', 'location_name': '北京-海淀区', 'money': '1.5-2万/月'}
14
{'title': '销售大区经理 - 照明', 'location_name': '北京', 'money': '1-1.2万/月'}
15
{'title': '总监助理', 'location_name': '北京', 'money': '6-8千/月'}
16
{'title': '行政前台', 'location_name': '北京-海淀区', 'money': '4-5千/月'}
17
{'title': '产品教育家,北京荟聚店 ID64063', 'location_name': '北京', 'money': '7-9千/月'}
18
{'title': '合规主任-华润三九', 'location_name': '北京', 'money': '1-1.5万/月'}
19
{'title': 'Product Marketing Mgr.- 产品市场经理(偏BD方向)', 'location_name': '北京', 'money': '2.2-3万/
月'}
20
{'title': '陈列师', 'location_name': '北京', 'money': None}
1
{'title': '投标主管(016219)', 'location_name': '北京', 'money': '1-1.5万/月'}
2
{'title': '销售运营主管/专员(北京)(J11102)', 'location_name': '北京-海淀区', 'money': '0.8-1万/月'}
3
{'title': 'Sales Associate 店员/营业员(王府井银泰)', 'location_name': '北京', 'money': None}
4
{'title': '销售助理(北京)', 'location_name': '北京-朝阳区', 'money': '5-7千/月'}
5
{'title': '汽车零配件采销员——北京', 'location_name': '北京-朝阳区', 'money': '4.5-7千/月'}
6
{'title': '新媒体运营专员', 'location_name': '北京-朝阳区', 'money': '0.8-1万/月'}
7
{'title': '物控专员(实习)', 'location_name': '北京', 'money': '3-5千/月'}
8
{'title': '销售工程师 半导体行业', 'location_name': '北京', 'money': None}
9
{'title': '高级销售工程师/客户经理', 'location_name': '北京-朝阳区', 'money': None}
10
{'title': '立项部主任', 'location_name': '北京-大兴区', 'money': '1-1.8万/月'}
11
{'title': 'Java开发工程师', 'location_name': '北京-海淀区', 'money': '0.8-1万/月'}
12
{'title': '推广经理', 'location_name': '北京-朝阳区', 'money': '1-2万/月'}
13
{'title': '航空航天类 英文学术期刊助理编辑', 'location_name': '北京-海淀区', 'money': '0.9-1.3万/月'}
14
{'title': '高级研发工程师', 'location_name': '北京-海淀区', 'money': '1.5-2.5万/月'}
15
{'title': '储备经理+无责底薪8K+带薪培训', 'location_name': '北京-朝阳区', 'money': '1-1.5万/月'}
16
{'title': '省区经理', 'location_name': '北京-昌平区', 'money': '4.5-6千/月'}
17
{'title': '医药销售代表/医学信息沟通专员(北京)', 'location_name': '北京', 'money': '0.5-2万/月'}
18
{'title': '法规专员', 'location_name': '北京-大兴区', 'money': '5-8千/月'}
19
{'title': '网络工程师', 'location_name': '北京-大兴区', 'money': '0.8-1万/月'}
20
{'title': '招投标专员', 'location_name': '北京-朝阳区', 'money': '0.8-1万/月'}
1
{'title': 'KA业务代表/主管', 'location_name': '北京-通州区', 'money': '0.8-1万/月'}
2
{'title': '运营专员(北京)', 'location_name': '北京-海淀区', 'money': '0.6-1万/月'}
3
{'title': 'JAVA高级开发工程师', 'location_name': '北京', 'money': '15-27.5万/年'}
4
{'title': '证券经纪人/客户经理', 'location_name': '北京-海淀区', 'money': '6-8千/月'}
5
{'title': '器件研发工程师(北京)', 'location_name': '北京', 'money': None}
6
{'title': 'Service Delivery Manager', 'location_name': '北京-东城区', 'money': '2-3万/月'}
7
{'title': 'java开发工程师 (MJ002986)', 'location_name': '北京-海淀区', 'money': '1-1.5万/月'}
8
{'title': '黄金珠宝导购/营业员', 'location_name': '北京-东城区', 'money': '4-6千/月'}
9
{'title': '商务bd', 'location_name': '北京', 'money': '1-1.5万/月'}
10
{'title': '技术部门助理(实习岗位)', 'location_name': '北京-朝阳区', 'money': '150元/天'}
11
{'title': '法务专员(J10649)', 'location_name': '北京-昌平区', 'money': '5-8千/月'}
12
{'title': '销售经理(北京)', 'location_name': '北京', 'money': '1-1.5万/月'}
13
{'title': '跨境电商总部-法务助理', 'location_name': '北京-顺义区', 'money': '6-8千/月'}
14
{'title': '医美业务经理', 'location_name': '异地招聘', 'money': '4.5-6千/月'}
15
{'title': '法律合规部——法务管理岗', 'location_name': '北京-西城区', 'money': '1-1.5万/月'}
16
{'title': '北京财务分析', 'location_name': '北京-朝阳区', 'money': '0.8-1万/月'}
17
{'title': '医学经理', 'location_name': '北京', 'money': '18-30万/年'}
18
{'title': '医药代表-成人-北京', 'location_name': '北京', 'money': None}
19
{'title': '注册主管/专员(肠内营养方向)', 'location_name': '北京', 'money': '1-1.5万/月'}
20
{'title': '资深前端开发工程师(J25116)', 'location_name': '北京-大兴区', 'money': '2-3万/月'}
1
{'title': '日料学徒', 'location_name': '北京-东城区', 'money': '4.5-6千/月'}
2
{'title': '法务主管/专员', 'location_name': '北京-东城区', 'money': '5-9千/月'}
3
{'title': 'KA财务管理', 'location_name': '北京-东城区', 'money': '2-2.5万/月'}
4
{'title': 'Investment Product Expert', 'location_name': '北京', 'money': '2-2.5万/月'}
5
{'title': '秀珀地坪产品销售(华北区域)', 'location_name': '北京', 'money': '1-1.5万/月'}
6
{'title': 'Future Trusted Advisor-Beijing', 'location_name': '北京', 'money': None}
7
{'title': '项目经理助理(北京)', 'location_name': '北京', 'money': None}
8
{'title': '设备工程师', 'location_name': '北京', 'money': '4.5-6千/月'}
9
{'title': '活动推广主管', 'location_name': '北京-丰台区', 'money': '6-8千/月'}
10
{'title': '前台接待(五矿广场)', 'location_name': '北京-东城区', 'money': '4.5-6千/月'}
11
{'title': '3D特效师', 'location_name': '北京-朝阳区', 'money': '1.2-2万/月'}
12
{'title': '高级人力资源专员(政策制度流程方向)--3614', 'location_name': '北京-海淀区', 'money': '1.2-1.5
万/月'}
13
{'title': '线上新渠道销售', 'location_name': '北京', 'money': '1-1.5万/月'}
14
{'title': '营运岗(北京)', 'location_name': '北京', 'money': '6-8万/年'}
15
{'title': '数据分析专员', 'location_name': '北京-大兴区', 'money': '6-9千/月'}
16
{'title': '售后服务主管', 'location_name': '北京-朝阳区', 'money': '1-1.5万/月'}
17
{'title': '销售工程师/Sales Engineer', 'location_name': '北京-朝阳区', 'money': '1-1.5万/月'}
18
{'title': '动画导演/高级分镜师', 'location_name': '北京-朝阳区', 'money': '2.5-3万/月'}
19
{'title': '北京各区域招后厨/打荷/配菜', 'location_name': '北京', 'money': '3.5-5千/月'}
20
{'title': '西餐厅副厨', 'location_name': '北京-西城区', 'money': '6-8千/月'}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值