600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Python爬虫实战 pytesseract模块 Python实现BOOS直聘拉勾网岗位数据可视化

Python爬虫实战 pytesseract模块 Python实现BOOS直聘拉勾网岗位数据可视化

时间:2023-12-21 07:47:08

相关推荐

Python爬虫实战 pytesseract模块 Python实现BOOS直聘拉勾网岗位数据可视化

前言

利用Python实现BOOS直聘&拉勾网岗位数据可视化。废话不多说。

让我们愉快地开始吧~

开发工具

Python版本:3.6.4

相关模块:

requests模块

pyspider模块;

pymysql模块;

pytesseract模块;

random模块;

re模块

以及一些Python自带的模块。

环境搭建

安装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。

在浏览器打开网址http://localhost:5000/,创建项目,添加项目名称,输入请求网址,得到如下图。

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

脚本编辑器具体代码如下

from pyspider.libs.base_handler import *import pymysqlimport randomimport timeimport recount = 0class 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)mit()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('/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 countcount += 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 class="vline"/>(.*?)<em class="vline"/>', 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 class="vline"/>(.*?)<em class="vline"/>', 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.hrefif 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 requestsimport pymysqlimport randomimport timeimport jsoncount = 0# 设置请求网址及请求头参数url = '/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': '','Origin': '','Referer': 'ttps:///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)mit()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 = {'first': 'false','pn': i,'kd': '数据分析'}response = requests.post(url=url, data=data, headers=headers)result = json.loads(response.text)job_messages = result['content']['positionResult']['result']for job in job_messages:global countcount += 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

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

工作技能图

工作福利词云图

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。