Chrome

服务器安装Chrome

1
2
3
4
# sudo apt-get install libxss1 libappindicator1 libindicator7
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome*.deb
# 如果报错,根据提示修复

Chrome headless的启动

1
google-chrome --headless --remote-debugging-port=9222 https://www.baidu.com --disable-gpu
param description
headless 无头模式
remote-debugging-port 调试端口
disable-gpu 禁止GPU

访问本地调试端口,应看到调试信息

1
curl http://localhost:9222

Chrome Driver

https://sites.google.com/a/chromium.org/chromedriver/downloads

http://npm.taobao.org/mirrors/chromedriver/

下载Chrome对应版本的Chrome Driver

报错

The process started from chrome location /usr/bin/google-chrome is no longer running
需要添加参数–disable-dev-shm-usage

测试

1
2
3
4
5
6
7
8
9
10
11
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
client = webdriver.Chrome(chrome_options=chrome_options,executable_path='path/to/chromedriver')
client.get("https://www.baidu.com")
content = client.page_source.encode('utf-8')
print(content)
client.quit()