一:首先在服务器上安装chrome和其对应的chromedriver。这个非常重要,一定要下载和安装其一一对应的版本。
安装chrome
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
sudo apt-get install -f
安装好之后我们看一下安装的版本
google-chrome --version
安装chromedriver, 安装对应chrome版本的chromedriver
Chromedriver的介绍
Chromedriver 也是一个能够被selenium驱动的浏览器,但是和PhantomJS的区别在于它是有界面的
下载地址:https://npm.taobao.org/mirrors/chromedriver
4 driver的安装
最简单的安装方式是:解压后把bin目录下的可执行文件移动到环境变量下,比如/usr/bin 或者是/usr/local/bin下面
----- Linux 下安装方式
chromedriver_linux64.zip : 版本 ChromeDriver v2.22 (2016-06-06)
(支持 Chrome v49-52,当前Ubuntu虚拟机下的Chrome版本为50.0)
1. unzip chromedriver_linux64.zip
2. sudo chmod +x chromedriver
3. sudo mv chromedriver /usr/local/bin/
解决截图中,中文出现方框问题
“https://wwwtypimg.oss-cn-beijing.aliyuncs.com/fonts/simsunttc.zip”
把文件放到 /usr/share/fonts/simsun.ttc
依次执行如下命令
mkfontdir
mkfontscale
fc-cache -fv
测试
from selenium import webdriver
if __name__ == '__main__':
options = webdriver.ChromeOptions()
# options.add_argument('--disable-dev-shm-usage')
# options.add_argument('--headless')
options.headless = True
driver = webdriver.Chrome(options=options)
# 发送请求
driver.get('https://www.baidu.com')
# 截屏获取
driver.save_screenshot('01baidu.png')
# 关闭页面
driver.close()
# 关闭浏览器
driver.quit()