在Ubuntu/debian Server 系统使用Chrome 无头浏览模式

这里说的Ubuntu/debian Server 系统指的是无界面的服务器,一般使用 ssh 登录操作。下面介绍在服务器系统安装chrome 浏览器及使用chrome headless 模式。

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

其它安装

下面是个人需要,做一下记录。

go 环境安装

https://golang.google.cn/dl/

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 转摘请注明来源

Suggested Topics

Ubuntu/Debian 查看CPU温度的方法

刚 DIY 一个小主机,因为使用功率很低的CPU,买了一个金钱豹2U散热器,风扇声音很大,就把风扇拆下来,只使用下面的铜块。就得对CPU问寒问暖,时刻了解它的温度。...

ubuntu/debian 下自行编译 OpenWRT 固件

首先声明,自行编译是一个自我折腾的过程,没时间或精力就不要试了,这里介绍个人尝试在ubuntu/debian 下编译OpenWRT固件和应用SDK 的过程。...

Ubuntu 或其它 GNU/Linux 系统下安装 Debian

家有老电脑不用就折腾不止,前段时间曾折腾一台老电脑,2007年上市,只支持 i386 系统,不支持U盘启动,在 winxp 系统下装了 Ubuntu,现在突发奇想,要在 Ubuntu 上安装 Debian。...

10分钟顺利升级 debian 系统 10 -> 11

debian 是小巧、方便使用的服务器系统,用命令行可以很方便升级。这里记录一下过程,是从 10/buster 升到 11/bullseye,其它版本也类似。...

Ubuntu 16.04.6 LTS 系统上安装 Python 3.6.3

自己的阿里云一个 VPS 用的是系统 Ubuntu 16.04.6 LTS,自带的python版本是 `2.7.12` 与 `3.5.2`,有时候要用到 python `3.6`,又不想卸掉原来版本。下面介绍安装 python 3.6.3 的过程,因为版本较旧,遇到一些坑,这里记录一下。...

Leave a Comment