在Ubuntu上安装Chrome浏览器和ChromeDriver

一.安装Chrome浏览器

1.安装依赖

1
sudo apt-get install libxss1 libappindicator1 libindicator7

2.下载Chrome安装包

1
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

3.安装

1
2
sudo dpkg -i google-chrome*.deb
sudo apt-get install -f

二.安装ChromeDriver

1.安装xvfb以便我们可以无头奔跑地运行Chrome

1
sudo apt-get install xvfb

2.安装依赖

1
sudo apt-get install unzip

3.下载安装包

1
wget -N http://chromedriver.storage.googleapis.com/对应版本/chromedriver_linux64.zip

 
chromedriver与chrome的对应关系表:
各个版本的下载地址:淘宝镜像

chromedriver版本 支持的Chrome版本
v2.41 v67-69
v2.40 v66-68
v2.39 v66-68
v2.38 v65-67
v2.37 v64-66
v2.36 v63-65
v2.35 v62-64
v2.34 v61-63
v2.33 v60-62
v2.32 v59-61
v2.31 v58-60
v2.30 v58-60
v2.29 v56-58
v2.28 v55-57
v2.27 v54-56
v2.26 v53-55
v2.25 v53-55
v2.24 v52-54
v2.23 v51-53
v2.22 v49-52
v2.21 v46-50
v2.20 v43-48
v2.19 v43-47
v2.18 v43-46
v2.17 v42-43
v2.13 v42-45
v2.15 v40-43
v2.14 v39-42
v2.13 v38-41
v2.12 v36-40
v2.11 v36-40
v2.10 v33-36
v2.9 v31-34
v2.8 v30-33
v2.7 v30-33
v2.6 v29-32
v2.5 v29-32
v2.4 v29-32

4.解压缩+添加执行权限

1
unzip chromedriver_linux64.zip

5.移动

1
sudo mv -f chromedriver /usr/local/share/chromedriver

6.建立软连接

1
2
sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver

三.无头运行Chrome

1.安装Python依赖

1
2
pip3 install selenium
pip3 install pyvirtualdisplay

2.开整

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from selenium import webdriver
from selenium.webdriver.chrome.options import Options 
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get(url)
iframe=driver.find_elements_by_tag_name('iframe')
    if len(iframe)>0:
        iframe = iframe[0]        
        driver.switch_to.frame(iframe)        
        ibody = driver.find_elements_by_tag_name('body')        
        if len(ibody)>0:            
            ibody=ibody[0]            
            video =ibody.find_element_by_tag_name('video')            
            if video is not None:                
                src = video.get_attribute("src")                
                if src !"" and src is not None:                    
                    res = {'src':src}                    
                    return str(res)