Published Date : 2020年9月9日8:07

【python】requestsとbs4で求人サイトのスクレイピングpandasでデータ処理してエクセルで保存
How to handle Selenium, which can be used for web scraping and also for RPA


This blog has an English translation


YouTubeにアップした動画、「【python】requestsとbs4で求人サイトのスクレイピングpandasでデータ処理してエクセルで保存」の補足説明の記事です。

Here's a little more about the 「【python】How to after scraping Job board with requests and bs4, data is processed with pandas and saved in Excel」 video I uploaded to YouTube.


目次

Table of Contents




① 動画の説明
① Video Description



requestsとbs4で求人サイトのスクレイピングpandasでデータ処理してエクセルで保存を説明します。

How to after scraping Job board with requests and bs4, data is processed with pandas and saved in Excel.

求人サイトからデータをスクレイピングする方法は前回のやり方とほぼ同じです。

The way to scrape data from job sites is pretty much the same as before.

import requests
from bs4 import BeautifulSoup
import re
import time

URL = "https://xxxxxxx/xxxxxxxxx"

res = requests.get(URL)

soup = BeautifulSoup(res.content, 'lxml')

正規表現やreplaceメソッドやstripメソッドを使って、無駄な文字を置き換えたり削除して、取得したデータをリストにします。

Use regular expressions and the replace and strip methods to replace or remove useless characters and list the retrieved data.

searchcountpage = re.search(r"\d+,?\d+", soup.select('div#searchCount')[0].get_text().strip())

ちなみに正規表現の[r"\d+,?\d+"]は、数字が一つ以上続いて、間にカンマが0個か1個あり、数字が一つ以上続いているパターンを意味しています。

By the way, the regular expression [r"\d+,?\d+"] is a pattern of one or more digits followed by zero or one comma between them, followed by one or more digits.

データをエクセルファイルに書き出す為に[openpyxl]をインストールします。

Install [openpyxl] to export data to an Excel file.

pip install openpyxl

Pandasを使って取得したデータをデータフレームに直して、そのデータフレームをエクセルファイルに書き出します。

Convert the data retrieved with Pandas into a data frame and export the data frame to an Excel file.

values = [salary, jobtype, location[:-1], company, title]
rows = {}
for idx, col in enumerate(cols):
    rows[col] = pd.Series(values[idx])

dftest = pd.DataFrame(rows, columns=cols)

dftest.to_excel('test.xlsx', sheet_name='Java ' +
searchcountpage, index=False)

シートの名前を調べたキーワードと件数にして分ければ見やすくなると思います。

I think it will be easier to see if you separate the names of the sheets by keywords and the number of sheets.

気を付けるのは、サイトのUIが変わったり、ポップアップが出る仕様だと、上手くコンテンツをスクレイピングできません。

It's important to note that if the UI of the site changes or if the site pops up, you won't be able to scrape the content of the site.

その場合は前回説明したseleniumを使用すると上手くいきます。

In that case, the previously described selenium works.

後は調べたいキーワードをリストにして、先ほどの処理を繰り返すだけです。

All you have to do is list the keywords you want to look up and repeat the process.

    salary = [s.get_text().replace("\n", "").replace("\u3000", "")
              for s in soup.select('div#SALARY ul li a')]
    jobtype = [j.get_text().replace("\n", "").replace("\u3000", "")
               for j in soup.select('div#JOB_TYPE ul li a')]
    location = [l.get_text().replace("\n", "").replace("\u3000", "")
                for l in soup.select('div#LOCATION ul li a')]
    company = [c.get_text().replace("\n", "").replace("\u3000", "")
               for c in soup.select('div#COMPANY ul li a')]
    title = [t.get_text().replace("\n", "").replace("\u3000", "")
             for t in soup.select('div#TITLE ul li a')]

[pd.ExcelWriter]を使えば、複数のデータフレームをまとめてエクセルファイルにすることができます。

With [pd.ExcelWriter], you can combine multiple data frames into an Excel file.

with pd.ExcelWriter('test.xlsx') as writer:
    for idx, pl in enumerate(pl_list):
        dfs[idx].to_excel(writer, sheet_name=pl +
                searchcountpages[idx], index=False)


以上です。お疲れ様です。

That's all. Thank you for your hard work.