这里说的Ubuntu/debian Server 系统指的是无界面的服务器,一般使用 ssh 登录操作。下面介绍在服务器系统安装chrome 浏览器及使用chrome headless 模式。
安装 chrome
下载 google chrome amd64当前最新稳定版
1
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
可以使用 apt install
安装
1
apt install ./google-chrome-stable_current_amd64.deb
这种安装方法会安装一些依赖库,并且会添加更新源,可以屏蔽它。
1
2
3
4
$ cat /etc/apt/sources.list.d/google-chrome.list
### THIS FILE IS AUTOMATICALLY CONFIGURED ###
# You may comment out this entry, but any other modifications may be lost.
# deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main
把文件 /etc/apt/sources.list.d/google-chrome.list
最后一行注释掉。
也可以使用 dpkg
安装,这种安装方法会忽略掉一些依赖,但我在使用中也没有出现问题。
1
2
3
4
5
apt-get install libxss1 libappindicator1 libindicator7
apt-get install curl
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
dpkg -i google-chrome*.deb
apt-get install -f
成功安装后看chrome信息,我是用上面第一种方式安装。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ apt info google-chrome-stable
Package: google-chrome-stable
Version: 80.0.3987.149-1
Status: install ok installed
Priority: optional
Section: web
Maintainer: Chrome Linux Team <chromium-dev@chromium.org>
Installed-Size: 224 MB
Provides: www-browser
Pre-Depends: dpkg (>= 1.14.0)
Depends: ca-certificates, fonts-liberation, libappindicator3-1, libasound2 (>= 1.0.16), libatk-bridge2.0-0 (>= 2.5.3), libatk1.0-0 (>= 2.2.0), libatspi2.0-0 (>= 2.9.90), libc6 (>= 2.16), libcairo2 (>= 1.6.0), libcups2 (>= 1.4.0), libdbus-1-3 (>= 1.5.12), libexpat1 (>= 2.0.1), libgcc1 (>= 1:3.0), libgdk-pixbuf2.0-0 (>= 2.22.0), libglib2.0-0 (>= 2.39.4), libgtk-3-0 (>= 3.9.10), libnspr4 (>= 2:4.9-2~), libnss3 (>= 2:3.22), libpango-1.0-0 (>= 1.14.0), libpangocairo-1.0-0 (>= 1.14.0), libx11-6 (>= 2:1.4.99.1), libx11-xcb1, libxcb1 (>= 1.6), libxcomposite1 (>= 1:0.3-1), libxcursor1 (>> 1.1.2), libxdamage1 (>= 1:1.1), libxext6, libxfixes3 (>= 1:5.0), libxi6 (>= 2:1.2.99.4), libxrandr2 (>= 2:1.2.99.3), libxrender1, libxss1, libxtst6, wget, xdg-utils (>= 1.0.2)
Recommends: libu2f-udev
Download-Size: unknown
APT-Manual-Installed: yes
APT-Sources: /var/lib/dpkg/status
Description: The web browser from Google
Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier.
用命令行测试
1
2
3
$ google-chrome --headless --disable-gpu --no-sandbox --remote-debugging-port=9222 https://chromium.org
DevTools listening on ws://127.0.0.1:9222/devtools/browser/7ad1e1f8-52f4-4a85-b616-a9bbd62daccd
必须加 --headless --disable-gpu --no-sandbox
这三个运行选项才能启动。
安装 chromedriver
我这里使用官方的驱动,
官方下载地址 https://sites.google.com/a/chromium.org/chromedriver/downloads
官方已转到 https://sites.google.com/chromium.org/driver/
选择上面安装的Chrome 版本,选择对应的系统,如 chromedriver_linux64.zip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ wget https://chromedriver.storage.googleapis.com/80.0.3987.106 /chromedriver_linux64.zip
$ unzip chromedriver_linux64.zip
$ cp chromedriver /usr/bin/
$ chromedriver -h
Usage: chromedriver [OPTIONS]
Options
--port=PORT port to listen on
--adb-port=PORT adb server port
--log-path=FILE write server log to file instead of stderr, increases log level to INFO
--log-level=LEVEL set log level: ALL, DEBUG, INFO, WARNING, SEVERE, OFF
--verbose log verbosely (equivalent to --log-level=ALL)
--silent log nothing (equivalent to --log-level=OFF)
--append-log append log file instead of rewriting
--replayable (experimental) log verbosely and don't truncate long strings so that the log can be replayed.
--version print the version number and exit
--url-base base URL path prefix for commands, e.g. wd/url
--readable-timestamp add readable timestamps to log
--disable-dev-shm-usage do not use /dev/shm (add this switch if seeing errors related to shared memory)
安装操作库
python 可以用 selenium
1
$ pip install selenium
golang 推荐用 tebeka/selenium
1
https://github.com/tebeka/selenium
如果用 python 驱动最低内存是256MB,否则 chrome 容易崩溃,推荐512MB以上。
用go 会省点内存和cpu,最低内存是256MB。
chromium
也可使用 chromium
- https://www.chromium.org/Home/
- https://download-chromium.appspot.com/
- https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html
- ChromeDriver https://chromedriver.chromium.org/downloads
其它安装
下面是个人需要,做一下记录。
go 环境安装
1
2
3
4
5
6
7
8
$ wget https://dl.google.com/go/go1.14.1.linux-amd64.tar.gz
$ tar -C /usr/local -xzf go1.14.1.linux-amd64.tar.gz
$ vi /etc/profile
export PATH=$PATH:/usr/local/go/bin
$ source /etc/profile
$ go -h
当go 库调用cgo 时,需要安装编译依赖
1
apt install build-essential
源码编译安装 OpenCC
1
2
3
4
5
6
7
git clone https://github.com/BYVoid/OpenCC.git
apt-get install cmake
apt-get install doxygen
cd OpenCC
make
make install
本文网址: https://pylist.com/topic/215.html 转摘请注明来源