拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 私藏项目实操分享,Python爬虫实作拉勾网岗位资料可视化

私藏项目实操分享,Python爬虫实作拉勾网岗位资料可视化

白鹭 - 2022-03-15 2036 0 0

前言

利用Python实作BOOS直聘&拉勾网岗位资料可视化,废话不多说,

让我们愉快地开始吧~

开发工具

Python版本:3.6.4

相关模块:

requests模块

pyspider模块;

pymysql模块;

pytesseract模块;

random模块;

re模块;

以及一些Python自带的模块,

环境搭建

很多人学习蟒蛇,不知道从何学起,

很多人学习python,掌握了基本语法之后,不知道在哪里寻找案例上手,

很多已经做了案例的人,却不知道如何去学习更多高深的知识,

那么针对这三类人,我给大家提供一个好的学习平台,免费获取视频教程,电子书,以及课程的源代码!

QQ群:101677771

欢迎加入,一起讨论一起学习

 

安装Python并添加到环境变量,pip安装需要的相关模块即可,

本次通过对BOSS直聘,拉勾网资料分析岗资料分析,了解资料分析岗的行业情况

网页分析

 

获取BOSS直聘索引页信息,主要是岗位名称、薪资、地点、作业年限、学历要求,公司名称、型别、状态、规模,

一开始是想对详情页分析的,还可以获取详情页里的作业内容和作业技能需求,

然后由于请求太多,就放弃了,索引页有10页,1页有30个岗位,一个详情页就需要一个请求,算起来一共有300个请求,

到了第2页(60个请求),就出现了访问过于频繁的警告,

而只获取索引页信息的话,只有10个请求,基本上没什么问题,外加也不想去鼓捣代理IP,所以来点简单的,

到时候做资料挖掘岗位的资料时,看看放慢时间能否获取成功,

 

获取拉勾网索引页信息,主要是岗位名称、地点、薪资、作业年限、学历要求,公司名称、型别、状态、规模,作业技能,作业福利,

网页为Ajax请求,采用PyCharm撰写代码,轻车熟路,

资料获取

pyspider获取BOSS直聘资料

pyspider的安装很简单,直接在命令列pip3 install pyspider即可,

这里因为之前没有安装pyspider对接的PhantomJS(处理JavaScript渲染的页面),

所以需要从网站下载下来它的exe档案,将其放入Python的exe档案所在的档案夹下,

最后在命令列输入pyspider all,即可运行pyspider,

在浏览器打开网址

,创建项目,添加项目名称,输入请求网址,得到如下图,

 

最后在pyspider的脚本编辑器里撰写代码,结合左边的反馈情况,对代码加以改正,

 

脚本编辑器具体代码如下

from pyspider.libs.base_handler import *
import pymysql
import random
import time
import re

count = 0

class Handler(BaseHandler):
    # 添加请求头,否则出现403报错
    crawl_config = {'headers': {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}}

    def __init__(self):
        # 连接数据库
        self.db = pymysql.connect(host='127.0.0.1', user='root', password='774110919', port=3306, db='boss_job', charset='utf8mb4')

    def add_Mysql(self, id, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people):
        # 将资料写入数据库中
        try:
            cursor = self.db.cursor()
            sql = 'insert into job(id, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people) values ("%d", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")' % (id, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people);
            print(sql)
            cursor.execute(sql)
            print(cursor.lastrowid)
            self.db.commit()
        except Exception as e:
            print(e)
            self.db.rollback()

    @every(minutes=24 * 60)
    def on_start(self):
        # 因为pyspider默认是HTTP请求,对于HTTPS(加密)请求,需要添加validate_cert=False,否则599/SSL报错
        self.crawl('https://www.zhipin.com/job_detail/?query=%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90&scity=100010000&industry=&position=', callback=self.index_page, validate_cert=False)

    @config(age=10 * 24 * 60 * 60)
    def index_page(self, response):
        time.sleep(random.randint(2, 5))
        for i in response.doc('li > div').items():
            # 设定全域变量
            global count
            count += 1
            # 岗位名称
            job_title = i('.job-title').text()
            print(job_title)
            # 岗位薪水
            job_salary = i('.red').text()
            print(job_salary)
            # 岗位地点
            city_result = re.search('(.*?)<em class=', i('.info-primary > p').html())
            job_city = city_result.group(1).split(' ')[0]
            print(job_city)
            # 岗位经验
            experience_result = re.search('<em />(.*?)<em />', i('.info-primary > p').html())
            job_experience = experience_result.group(1)
            print(job_experience)
            # 岗位学历
            job_education = i('.info-primary > p').text().replace(' ', '').replace(city_result.group(1).replace(' ', ''), '').replace(experience_result.group(1).replace(' ', ''),'')
            print(job_education)
            # 公司名称
            company_name = i('.info-company a').text()
            print(company_name)
            # 公司型别
            company_type_result = re.search('(.*?)<em class=', i('.info-company p').html())
            company_type = company_type_result.group(1)
            print(company_type)
            # 公司状态
            company_status_result = re.search('<em />(.*?)<em />', i('.info-company p').html())
            if company_status_result:
                company_status = company_status_result.group(1)
            else:
                company_status = '无信息'
            print(company_status)
            # 公司规模
            company_people = i('.info-company p').text().replace(company_type, '').replace(company_status,'')
            print(company_people + '\n')
            # 写入数据库中
            self.add_Mysql(count, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people)
        # 获取下一页信息
        next = response.doc('.next').attr.href
        if next != 'javascript:;':
            self.crawl(next, callback=self.index_page, validate_cert=False)
        else:
            print("The Work is Done")
        # 详情页信息获取,由于访问次数有限制,不使用
        #for each in response.doc('.name > a').items():
            #url = each.attr.href
            #self.crawl(each.attr.href, callback=self.detail_page, validate_cert=False)

    @config(priority=2)
    def detail_page(self, response):
        # 详情页信息获取,由于访问次数有限制,不使用
        message_job = response.doc('div > .info-primary > p').text()
        city_result = re.findall('城市:(.*?)经验', message_job)
        experience_result = re.findall('经验:(.*?)学历', message_job)
        education_result = re.findall('学历:(.*)', message_job)

        message_company = response.doc('.info-company > p').text().replace(response.doc('.info-company > p > a').text(),'')
        status_result = re.findall('(.*?)\d', message_company.split(' ')[0])
        people_result = message_company.split(' ')[0].replace(status_result[0], '')

        return {
            "job_title": response.doc('h1').text(),
            "job_salary": response.doc('.info-primary .badge').text(),
            "job_city": city_result[0],
            "job_experience": experience_result[0],
            "job_education": education_result[0],
            "job_skills": response.doc('.info-primary > .job-tags > span').text(),
            "job_detail": response.doc('div').filter('.text').eq(0).text().replace('\n', ''),
            "company_name": response.doc('.info-company > .name > a').text(),
            "company_status": status_result[0],
            "company_people": people_result,
            "company_type": response.doc('.info-company > p > a').text(),
        }

获取BOSS直聘资料分析岗资料

 

PyCharm获取拉勾网资料

import requests
import pymysql
import random
import time
import json

count = 0
# 设定请求网址及请求头自变量
url = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
    'Cookie': '你的Cookie值',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Connection': 'keep-alive',
    'Host': 'www.lagou.com',
    'Origin': 'https://www.lagou.com',
    'Referer': 'ttps://www.lagou.com/jobs/list_%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90?labelWords=sug&fromSearch=true&suginput=shuju'
}

# 连接数据库
db = pymysql.connect(host='127.0.0.1', user='root', password='774110919', port=3306, db='lagou_job', charset='utf8mb4')

def add_Mysql(id, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people, job_tips, job_welfare):
    # 将资料写入数据库中
    try:
        cursor = db.cursor()
        sql = 'insert into job(id, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people, job_tips, job_welfare) values ("%d", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")' % (id, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people, job_tips, job_welfare);
        print(sql)
        cursor.execute(sql)
        print(cursor.lastrowid)
        db.commit()
    except Exception as e:
        print(e)
        db.rollback()

def get_message():
    for i in range(1, 31):
        print('第' + str(i) + '页')
        time.sleep(random.randint(10, 20))
        data = https://www.cnblogs.com/sn520/p/{
            'first': 'false',
            'pn': i,
            'kd': '资料分析'
        }
        response = requests.post(url=url, data=https://www.cnblogs.com/sn520/p/data, headers=headers)
        result = json.loads(response.text)
        job_messages = result['content']['positionResult']['result']
        for job in job_messages:
            global count
            count += 1
            # 岗位名称
            job_title = job['positionName']
            print(job_title)
            # 岗位薪水
            job_salary = job['salary']
            print(job_salary)
            # 岗位地点
            job_city = job['city']
            print(job_city)
            # 岗位经验
            job_experience = job['workYear']
            print(job_experience)
            # 岗位学历
            job_education = job['education']
            print(job_education)
            # 公司名称
            company_name = job['companyShortName']
            print(company_name)
            # 公司型别
            company_type = job['industryField']
            print(company_type)
            # 公司状态
            company_status = job['financeStage']
            print(company_status)
            # 公司规模
            company_people = job['companySize']
            print(company_people)
            # 作业技能
            if len(job['positionLables']) > 0:
                job_tips = ','.join(job['positionLables'])
            else:
                job_tips = 'None'
            print(job_tips)
            # 作业福利
            job_welfare = job['positionAdvantage']
            print(job_welfare + '\n\n')
            # 写入数据库
            add_Mysql(count, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people, job_tips, job_welfare)

if __name__ == '__main__':
    get_message()

获取拉勾网资料分析岗资料

 

资料可视化

城市分布图

 

 

城市分布热力图

 

 

作业经验薪水图

 

 

这里通过看箱形图的四分位及中间值,大致能看出随着作业年限的增长,薪资也是一路上升,

BOSS直聘里,1年以内作业经验的薪资,有个最高4万多的,这肯定是不合理的,

于是就去数据库看了下,其实那个岗位要求是3年以上,但实际给的标签却是1年以内,

所以说资料来源提供的 资料的准确性 很重要,

学历薪水图

 

 

总的来说「硕士」>「本科」>「大专」,当然大专、本科中也有高薪水的,

毕竟越往后能力就越重要,学历算是一个 重要的加分项 ,

公司状态薪水图

 

 

公司规模薪水图

 

 

正常来说,公司规模越大,薪水应该会越高,

毕竟大厂的工资摆在那里,想不知道都难,

公司型别TOP10

 

 

资料分析岗主要集中在互联网行业,「金融」「地产」「教育」「医疗」「游戏」也有所涉及,

作业技能图

 

作业福利词云图

 

这里可以看出大部分重点都围绕着「五险一金」「福利多」「团队氛围好」「晋升空间大」「行业大牛领头」上,

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *