Kohei Blog

夫・父親・医療系エンジニア

【Python】スクレイピングでエンジニア言語別給料幅を集計してみた

f:id:kei_01011:20210516153928p:plain
求人情報

プログラミング言語って、結局どの言語を選択すれば給料が上がるんだろ? 素朴な疑問から、求人サイトをスクレイピングして集計してみました。

集計方法

GREENの求人サイトから集計

バックエンド、データ分析系、フロントエンドの言語で検索し、それぞれの求人情報のタグ、最低給料、最大給料を取得して集計

コード

import requests
from bs4 import BeautifulSoup
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep

def get_green_items(keyword):
    
    # driver and beautifulsoup
    options = Options()
    options.add_argument('--headless')
    driver = webdriver.Chrome('/Users/user/driver/chromedriver',options=options)

    pages = '1'
    url = f"https://www.green-japan.com/search_key/01?keyword={keyword}&page={pages}"

    #html取得
    driver.get(url)
    res = requests.get(url)
    soup = BeautifulSoup(res.text,'html.parser')

    # 検索結果件数からページ数を算出
    post_count = soup.select('.pagers')
    max_page = int(post_count[0].find_all('a')[5].text)
    print(max_page)
    # ページ数の数だけループ|
    list_all = []

    for page in range(1, (max_page + 1)):
        try:
            options = Options()
            options.add_argument('--headless')
            driver = webdriver.Chrome('/Users/user/driver/chromedriver',options=options)

            pages = page
            url = f"https://www.green-japan.com/search_key/01?keyword={keyword}&page={pages}"

            driver.get(url)
            res = requests.get(url)
            soup = BeautifulSoup(res.text,'html.parser')

            print(url)
            # get htmltag
            offer = soup.find_all('div',{'class':'job-offer-icon'})
            html_salary = soup.find_all('ul',{'class','job-offer-meta-tags'})
            company = soup.find_all('h3',{'class':'card-info__detail-area__box__title'})
            html_sub_title = soup.find_all('div',{'class':'card-info__detail-area__box__sub-title'})
            html_tag_lists = soup.find_all('ul',{'class':'tag-gray-area'})
            html_href = soup.find_all('a',{'class':'js-search-result-box'})
#             'https://www.green-japan.com/' + 
            # 取得したページhtmlからタグ取得、CSV吐き出し
            print(f"{page}page:start...")
        except:
            print('error')

        # 1ページ分のデータをリストに格納
        for i in range(10):
            data_list = []
            tags = []

            try:
                # HTMLタグがない場合があるので条件分岐させる
                salary = html_salary[i].find('span').next_sibling.replace('\n','').replace(' ','').replace('万円','').split('〜')
                salary_check = html_salary[i].find_all('span',{'class':'icon-salary'})
                if len(salary_check) > 0:
                    min_salary = salary[0]
                    max_salary = salary[1]
                else:
                    min_salary = ''
                    max_salary = ''

                sub_title = html_sub_title[i].find_all('span')
                for title in sub_title:
                    if '設立年月日' in title.text:
                        build = title.text.replace('設立年月日 ','').replace('年','/').replace('月','')
                    elif '従業員数' in title.text:
                        staff = title.text.replace('従業員数 ','').replace('人','')
                    elif '平均年齢' in title.text:
                        age = title.text.replace('平均年齢','').replace('歳','')
                    else:
                        pass

                tag_lists = html_tag_lists[i].find_all('span')
                
                url = 'https://www.green-japan.com/' + html_href[i]['href']
                for tag in tag_lists:
                    tags.append(tag.text)
                tag_list = ','.join(tags)

                data_list = [
                    company[i].text,
                    offer[i].text,
                    min_salary,
                    max_salary,
                    build,
                    staff,
                    age,
                    tag_list,
                    url
                ]
                list_all.append(data_list)
                time.sleep(5)
            except:
                print('page error')
                pass

        print(f"{page}page:done...!")
        time.sleep(8)
        
    columns = ['企業名','オファータイトル','給料最小値','給料最大値','設立年月日','従業員数','平均年齢','タグ','URL']
    df = pd.DataFrame(list_all,columns=columns)
    return df

get_green_items('Python')で実行。

今回、seleniumを使ってみたかっただけなので、わざわざseleniumを使う必要はない。

BeautifulSoupで十分収集可能だと思う。

収集したデータはスプレッドシートなりに吐き出して、Tableauで可視化。

今回は、企業の個別情報も含んでいるのでTableauPublicにパブリッシュはしていません。