2024年用scrapy爬取BOSS直聘的操作

SCrapy框架实现对BOSS直聘的爬取

对SCrapy框架的一个简单认识

在这里插入图片描述

Scrapy 组件的作用

  1. Engine(引擎):负责控制系统所有组件之间的数据流,并在发生某些操作时触发事件。它是整个爬虫的核心。

  2. Scheduler(调度器):接收引擎的请求,对请求去重并放入队列中。当引擎请求新的请求时,将队列中的请求按顺序返回给引擎。

  3. Downloader(下载器):负责获取网页数据并将其返回给引擎,引擎再将数据传给 Spiders(爬虫)。

  4. Spiders(爬虫):解析响应,从中提取出 Items(项目)和新的 Requests(请求)。

  5. Item Pipeline(项目管道):处理爬虫提取的项目,包括清理、验证和存储项目。

  6. Downloader middlewares(下载器中间件):处理从引擎到下载器的请求和从下载器到引擎的响应,用于在这些过程中修改或替换请求和响应。

  7. Spider middlewares(爬虫中间件):处理爬虫的输入(响应)和输出(项目和请求),可以对这些数据进行修改、添加或删除。

Scrapy 数据流

  1. 初始请求

    • Scrapy 会实例化一个 Crawler 对象,在该对象中创建 Spider 对象和 Engine 对象。
    • 通过 Engine 对象打开 Spider,并生成第一个 request(请求)。
  2. 请求处理流程

    • 步骤 1:Engine 从 Spiders 获取初始请求。
    • 步骤 2:Engine 把请求给调度器,并询问下一次请求。
    • 步骤 3:Scheduler 对 URL 去重,放到队列中等待,并把下一个 request 返回给 Engine。
    • 步骤 4:Engine 把从调度器返回的 request 经过下载中间件交给下载器。
    • 步骤 5:Downloader 下载页面后生成一个 Response(响应),并通过下载器中间件将其发送到 Engine。
    • 步骤 6:Engine 接收响应,并通过爬虫中间件将其发送到爬虫进行处理。
    • 步骤 7:爬虫接收到响应,解析处理响应,提取出 Items 和 新的 Requests,再通过爬虫中间件提交给 Engine。
    • 步骤 8:Engine 把接收到的 Items 提交给 Item Pipeline,把接收到的 Requests 提交给调度器。
  3. 重复过程

    • 重复上述步骤,直到 Scheduler 中没有请求为止。

拿到一个网站爬取需求首先需要进行分析网站的反爬措施,再根据反爬漏洞想到对应的解决方法

1. 测试反爬

import time
from random import randint, choice
import requests

url = 'https://ja.58.com/job.shtml?utm_source=sem-baidu-pc&spm=u-2few7p4vh988mb62t1.2few8w827wgt4eurg.kd_201345177084.cr_43861026238.ac_20304970.cd_11302497077865040299'

user_agents = [
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0',
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.0.0',
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36 Edg/100.0.0.0'
]

headers = {
    'User-Agent': choice(user_agents)
}

response = requests.get(url=url, headers=headers)

with open('test.html', 'w', encoding='utf-8') as f:
    f.write(response.text)

# 随机延时 1 到 5 秒
time.sleep(randint(1, 5))

在这里插入图片描述

可知直接爬取会被屏蔽,但是正常打开没有问题

在这里插入图片描述

因此想到用来selenium库来进行打开操作同时获取cookie保存

 def getcookie(self, url, cookies):
        driver = webdriver.Chrome()
        driver.get(url)
        time.sleep(6)
        dict_cookies = driver.get_cookies()
        json_cookies = json.dumps(dict_cookies)
        with open(cookies, "w") as fp:
            fp.write(json_cookies)
            print('Cookies保存成功!')
        driver.quit()
def load_cookies(self):
        with open(self.cookie_file, "r") as fp:
            cookies = json.load(fp)
        for cookie in cookies:
            if 'domain' in cookie:
                del cookie['domain']
            self.driver.add_cookie(cookie)

2. 定义一个下载中间件类,截取spiders的请求(中间件直接截取请求,并且返回给Spider进行数据解析)

class SeleniumMiddleware:
    query = ""
    city_id = ""

    def __init__(self):
        self.cookie_file = 'boss_cookies.json'
        # 检查文件是否存在,如果不存在则创建一个空文件
        if not os.path.exists(self.cookie_file):
            with open(self.cookie_file, 'w') as f:
                pass
        self.getcookie('https://www.zhipin.com/web/geek/job-recommend', self.cookie_file)
        self.driver = webdriver.Chrome()

    def getcookie(self, url, cookies):
		#此处省略
    def load_cookies(self):
		#此处省略

    def process_request(self, request, spider):
        try:
            if request.meta.get('first_request', True):
                qe = input('请搜索岗位和城市id(空格隔开):').split(' ')
                self.query = qe[0]
                self.city_id = qe[1]
                target_url = f"https://www.zhipin.com/web/geek/job?query={self.query}&city={self.city_id}&page=1"
                q: str = self.query
                c = self.city_id
                request.meta['first_request'] = False
            else:
                page = int(request.meta.get('page_number'))
                target_url = f"https://www.zhipin.com/web/geek/job?query={self.query}&city={self.city_id}&page={page}"
            print(f"Fetching URL: {target_url}")
            self.driver.get(target_url)
            self.load_cookies()
            self.driver.refresh()

            WebDriverWait(self.driver, 20).until(
                EC.presence_of_element_located((By.CLASS_NAME, "job-card-wrapper"))
            )

            data = self.driver.page_source
            return HtmlResponse(url=request.url, body=data, encoding='utf-8', request=request)
        except Exception as e:
            print(f"An error occurred: {e}")
            return HtmlResponse(url=request.url, status=500, request=request)

    def __del__(self):
        if self.driver:
            self.driver.quit()

请求代码

  WebDriverWait(self.driver, 20).until(
                EC.presence_of_element_located((By.CLASS_NAME, "job-card-wrapper"))
            )

因为boss直聘具有反爬操作,很多时候能够检测出来不是正常用户,需要用该方法反复进行请求操作直到html页面中能够获取我们想要的标签内容(这个标签下的很多数据都是我们需要进行爬取操作的数据)

数据解析操作

使用Xpath来进行,同时对空数据进行处理

import scrapy
from ..items import BossItem


class BossSpider(scrapy.Spider):
    name = "boss"
    allowed_domains = ["www.zhipin.com"]
    start_urls = ["https://www.zhipin.com/"]
    page = 1

    def parse(self, response):
        with open('test.html', 'w', encoding='utf-8') as f:
            f.write(response.text)

        # 改进的XPath表达式
        li_list = response.xpath('//li[@class="job-card-wrapper"]')
        print(f"Number of items found: {len(li_list)}===============================================")

        for li in li_list:
            title = li.xpath(".//span[@class='job-name']/text()").extract_first() or ''
            salary = li.xpath(".//span[@class='salary']/text()").extract_first() or ''
            area = li.xpath(".//span[@class='job-area']/text()").extract_first() or ''

            # 确保提取job_lable_list的正确性
            job_lable_list = li.xpath(".//ul[@class='tag-list']//text()").extract()
            if len(job_lable_list) >= 2:
                experience = job_lable_list[0] or ''
                education = job_lable_list[1] or ''
            else:
                experience = ''
                education = ''

            company = li.xpath(".//h3[@class='company-name']/a/text()").extract_first() or ''

            # 确保提取company_message的正确性
            company_message = li.xpath(".//ul[@class='company-tag-list']//text()").extract()
            company_type = company_message[0] if company_message else ''

            # 提取boon字段
            boon = li.xpath('.//div[@class="job_card_footer"]//div[@class="info-desc"]/text()').extract()
            boon = boon[0] if boon else None
            # 技能
            skill_list = li.xpath(
                ".//div[@class='job-card-footer clearfix']//ul[@class='tag-list']/li/text()").extract() or []
            skill = "|".join(skill_list)
            # 创建BossItem对象并传递数据
            book = BossItem(
                title=title,
                address=area,
                salary=salary,
                experience=experience,
                education=education,
                company=company,
                companyType=company_type,
                skill_list=skill,
            )
            yield book

        if self.page < 10:
            self.page += 1
            next_url = f"https://www.zhipin.com/web/geek/job?query=java&city=101210100&page={self.page}"
            yield scrapy.Request(
                url=next_url,
                callback=self.parse,
                meta={'page_number': self.page, 'first_request': False}
            )

spider/boss.py中代码的注意事项

        if self.page < 10:
            self.page += 1
            next_url = f"https://www.zhipin.com/web/geek/job?query=java&city=101210100&page={self.page}"
            yield scrapy.Request(
                url=next_url,
                callback=self.parse,
                meta={'page_number': self.page, 'first_request': False}
            )

注意:此代码next_url传过去不会真正的被下载器中间件处理,而是为了防止不报错而进行的(不知道哪错了)

中间件部分代码

 if request.meta.get('first_request', True):
                qe = input('请搜索岗位和城市id(空格隔开):').split(' ')
                self.query = qe[0]
                self.city_id = qe[1]
                target_url = f"https://www.zhipin.com/web/geek/job?query={self.query}&city={self.city_id}&page=1"
                q: str = self.query
                c = self.city_id
                request.meta['first_request'] = False
            else:
                page = int(request.meta.get('page_number'))
                target_url = f"https://www.zhipin.com/web/geek/job?query={self.query}&city={self.city_id}&page={page}"

由于csdn下载代码需要开启vip具体代码实现可以访问我的github:

最新BOSS直聘爬取以及其他常见网站爬取 (github.com)

### 使用Scrapy框架爬取Boss网站信息 #### 准备工作 为了成功利用Scrapy框架爬取Boss网站的信息,需先安装并配置好Python环境以及Scrapy库。确保已正确安装Scrapy版本,并熟悉基本命令操作。 #### 创建项目结构 启动一个新的Scrapy项目用于处理此次任务: ```bash scrapy startproject boss_zhipin_spider cd boss_zhipin_spider ``` 接着,在`spiders`文件夹内新建具体的Spider类来定义目标网页的解析逻辑[^3]。 #### 设置请求头 由于现代Web应用通常会检测访问者的User-Agent等HTTP头部字段以防止自动化工具滥用API接口或模拟浏览器行为不当,因此建议在项目的`settings.py`里适当调整默认发送出去的Headers参数,使其更接近真实用户的浏览习惯[^4]: ```python USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' ROBOTSTXT_OBEY = False DOWNLOAD_DELAY = 3 CONCURRENT_REQUESTS_PER_DOMAIN = 8 COOKIES_ENABLED = True DEFAULT_REQUEST_HEADERS = { 'Accept': '*/*', 'Connection': 'keep-alive', } ``` 这里设置了较为通用的浏览器标识符(User Agent),关闭了对robots.txt规则集的遵循(因为某些站点可能会阻止爬虫活动),增加了下载延迟时间减少服务器压力,限制同一域名下的并发请求数量保护资源不被过度占用,启用了Cookie支持以便维持登录状态或其他依赖于Session机制的功能正常运作。 #### 编写爬虫代码 下面给出一个简单的例子展示如何编写针对特定页面类型的Spider脚本: ```python import scrapy from ..items import BossZhipinItem class JobSpider(scrapy.Spider): name = "jobs" allowed_domains = ["www.zhipin.com"] start_urls = ['https://www.zhipin.com/job_detail/?query=Python&city=101010100'] def parse(self, response): items = [] for sel in response.xpath('//div[@class="job-list"]/ul/li'): item = BossZhipinItem() try: item['title'] = sel.css('a::attr(title)').get().strip() item['salary'] = sel.xpath('.//span[@class="red"]//text()').extract_first('').strip() item['company_name'] = sel.css('.info-company .name a::attr(title)').get().strip() yield item except AttributeError as e: continue next_page_url = response.css('a.next::attr(href)').get() if next_page_url is not None: yield scrapy.Request(response.urljoin(next_page_url)) ``` 上述代码片段实现了对于给定URL列表中的每一页进行遍历读取,并提取出所需的关键属性保存到自定义的数据容器(`BossZhipinItem`)当中;同时还会尝试获取下一页链接继续深入挖掘更多记录到全部完成为止[^1]。 #### 数据存储 最后一步就是考虑怎样持久化收集来的资料了。可以采用多种方式实现这一点,比如接打印输出至控制台、导出JSON/XML文档形式或是接入关系型数据库管理系统(MySQL为例)。具体做法取决于实际应用场景和个人偏好。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值