LoginSignup
3

More than 3 years have passed since last update.

posted at

updated at

CentOS7でPHP + facebook/php-webdriverでchromeを動かしてクローリング

背景

ある日、クローラが必要になった :smirk:

調べてみると、なんとなくPython + Selenium が主流なのかな... :open_mouth:

普段 Laravelを使っているし、PHPでなんとかしたいなぁ...
と、さらに調べたら、以下の組み合わせでいけそう :thumbsup_tone2:

facebook/php-webdriver + chrome driver

一度Laravel duskも入れてみましたが、きっと似たような構成かと思われます。
今回はテスト用でもないので、facebook/php-webdriverのみ入れて使ってみました :ok_woman:

今回は、その環境構築の手順メモです :writing_hand_tone2:

手順

試した環境

CentOS 7.7

1.Chromeのインストール

$ sudo yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

バージョン確認 :point_up_2_tone2:

$ google-chrome --version
Google Chrome 79.0.3945.130 

できましたね :wink::metal_tone2:

2.ChromeDriverのインストール

Chromeをリモート操作するため、まずはChromeDriverを入手します。

▼ ChromeDriver - WebDriver for Chrome
https://sites.google.com/a/chromium.org/chromedriver/downloads

ChromeDriverはChromeのバージョンに合わせないとエラーが起きるようでした :angel:
今回は「Google Chrome 79」用に下記を取得です。

$ wget https://chromedriver.storage.googleapis.com/79.0.3945.36/chromedriver_linux64.zip
$ unzip chromedriver_linux64.zip
$ sudo mv chromedriver /usr/local/bin/

3.php-webdriverのインストール

▼php-webdriver
https://github.com/php-webdriver/php-webdriver

これは普通にcomposerでプロジェクトに追加です :slight_smile:

composer require php-webdriver/webdriver

とりあえず、準備は以上であります:muscle_tone2:

補足

上記でスクリーンショット撮影のサンプルを作ったら、取得したキャプチャの日本語が文字化けしていたので、フォントを追加 :point_up_tone2:

sudo yum -y install ipa-gothic-fonts ipa-mincho-fonts ipa-pgothic-fonts ipa-pmincho-fonts
fc-cache -fv

文字化けというか、フォントがなければ、それはそうですね・・・ :sweat_smile:

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
3