1. 程式人生 > 其它 >私藏專案實操分享,Python爬蟲實現拉勾網崗位資料視覺化

私藏專案實操分享,Python爬蟲實現拉勾網崗位資料視覺化

尊重原創版權: https://www.gewuweb.com/hot/17163.html

私藏專案實操分享,Python爬蟲實現拉勾網崗位資料視覺化

uQDIgQcZ4qBt2GcZMS_LrCeB/?source=tuwen_detail)

前言

利用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 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 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.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 = {
            '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 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

資料分析崗主要集中在網際網路行業,「金融」「地產」「教育」「醫療」「遊戲」也有所涉及。

工作技能圖

工作福利詞雲圖

這裡可以看出大部分重點都圍繞著「五險一金」「福利多」「團隊氛圍好」「晉升空間大」「行業大牛領頭」上。

間大」「行業大牛領頭」上。

更多內容參考: https://www.gewuweb.com/sitemap.html