CentOS7にGoogle Chromeをインストールします。 Chromiumをインストールする場合は、以下を参照。
インストール
Google Chromeの安定版をrpmからインストールします。
$ sudo yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
または、 以下の内容で /etc/yum.repos.d/google-chrome.repo を作成して
[google-chrome] name=google-chrome baseurl=http://dl.google.com/linux/chrome/rpm/stable/x86_64 enabled=1 gpgcheck=1 gpgkey=https://dl.google.com/linux/linux_signing_key.pub以下を実行してyumでインストールします。
$ sudo yum install google-chrome-stable
インストールが完了したら、バージョンを確認しておきます。2018年7月時点では以下のようになりました。
$ google-chrome --version Google Chrome 67.0.3396.99インストール先は、以下のようになっていました。
$ which google-chrome /usr/bin/google-chrome $ ls -l /usr/bin/google-chrome lrwxrwxrwx 1 root root 31 7月 10 21:22 /usr/bin/google-chrome -> /etc/alternatives/google-chrome
日本語フォント
日本語フォントが無いとエラーになる場合があるようなのでインストールします。
$ sudo yum install ipa-gothic-fonts ipa-mincho-fonts ipa-pgothic-fonts ipa-pmincho-fonts
ヘッドレスでの動作確認
インストールが正常に完了したら、ヘッドレスでアクセスしてみます。 ヘッドレスの場合はオプションに"--headless"を指定します。 また正しくアクセスできたか確認するため、取得HTMLをダンプするオプションの"--dump-dom"を指定します。
http://example.com/ にアクセスして、以下のようになれば成功です。 エラーメッセージが表示されていますが、とりあえずアクセスできているので無視します。
$ google-chrome --headless --dump-dom http://example.com/
[0710/221658.627014:ERROR:gpu_process_transport_factory.cc(1017)] Lost UI shared context.
<!DOCTYPE html>
<html><head>
<title>Example Domain</title>
<meta charset="utf-8">
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 50px;
background-color: #fff;
border-radius: 1em;
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
body {
background-color: #fff;
}
div {
width: auto;
margin: 0 auto;
border-radius: 0;
padding: 1em;
}
}
</style>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is established to be used for illustrative examples in documents. You may use this
domain in examples without prior coordination or asking for permission.</p>
<p><a href="http://www.iana.org/domains/example">More information...</a></p>
</div>
</body></html>
使用方法の参考ページ
トラブル
fonts.confの<blank>エラー
以下のようにfonts.confのエラーが発生する場合
$ google-chrome --headless --dump-dom http://example.com/ [0710/212252.959768:ERROR:gpu_process_transport_factory.cc(1017)] Lost UI shared context. Fontconfig warning: "/etc/fonts/fonts.conf", line 146: blank doesn't take any effect anymore. please remove it from your fonts.conf Failed to generate minidump.Illegal instruction以下のようになっている/etc/fonts/fonts.confから、"<blank>"と"</blank>"を削除します。
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
...
...
<!--
These are the default Unicode chars that are expected to be blank
in fonts. All other blank chars are assumed to be broken and
won't appear in the resulting charsets
-->
<blank>
<int>0x0020</int> <!-- SPACE -->
<int>0x00A0</int> <!-- NO-BREAK SPACE -->
...
...
<int>0xFFFA</int> <!-- INTERLINEAR ANNOTATION SEPARATOR -->
<int>0xFFFB</int> <!-- INTERLINEAR ANNOTATION TERMINATOR -->
</blank>
<!--
Rescan configuration every 30 seconds when FcFontSetList is called
-->
...
...
</fontconfig>
ダンプで何も表示されない
以下のようにERRORが表示されるだけ指定URLのDOMが表示されない場合、日本語フォントをインストールしてみます。
$ google-chrome --headless --dump-dom http://example.com/ [0710/215346.723946:ERROR:gpu_process_transport_factory.cc(1017)] Lost UI shared context.
日本語フォントのインストール
$ sudo yum install ipa-gothic-fonts ipa-mincho-fonts ipa-pgothic-fonts ipa-pmincho-fonts
Chromeのバージョンが古い場合やWindowsの場合は、起動オプションに"--disable-gpu"を指定してみます。
$ google-chrome --headless --disable-gpu --dump-dom http://example.com/