谷歌浏览器各平台各版本资源

https://chromedriver.storage.googleapis.com/index.html    各版本各平台浏览器驱动下载地址(官网)

https://npm.taobao.org/mirrors/chromedriver/   各版本各平台浏览器驱动下载地址(阿里镜像)

https://www.chromedownloads.net/chrome64win/    各版本各平台chrom下载地址

各版本驱动对应关系:

ChromeDriver Version     Chrome Version
78.0.3904.11         78
77.0.3865.40         77
77.0.3865.10         77
76.0.3809.126         76
76.0.3809.68         76
76.0.3809.25         76
76.0.3809.12         76
75.0.3770.90         75
75.0.3770.8         75
74.0.3729.6         74
73.0.3683.68         73
72.0.3626.69         72
2.46           71-73
2.46           71-73
2.45            70-72
2.44     69-71
2.43     69-71
2.42     68-70
2.41     67-69
2.40     66-68
2.39     66-68
2.38     65-67
2.37     64-66
2.36     63-65
2.35     62-64

 python 测试用谷歌浏览时,把chromedriver.exe文件放到python安装目录下的\Scripts下即可。

在用selenium做爬虫的时候,需要用到谷歌或者火狐浏览器(CentoOS 7环境,无头模式)

安装谷歌浏览器及驱动的方式:

step1. 在官网或者镜像网站下载chrome的rpm包,例如:google-chrome-stable_current_x86_64.rpm

step2. 下载版本对应的驱动: chromedriver_linux64.zip

step3. 安装依赖包:

yum install -y lsb
yum install -y libXScrnSaver

yum install libappindicator-gtk3

yum install liberation-fonts

step 4 安装浏览器程序

rpm -ivh google-chrome-stable_current_x86_64.rpm

也可以在线安装:

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

step 5 安装驱动。解压驱动后,放在目录   /usr/local/bin/  下才能生效

安装完成后,测试安装:

google-chrome-stable --headless --disable-gpu --screenshot http://www.baidu.com/

报错:

[1209/235648.782239:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

需要指定以no-sandbox方式运行。

google-chrome-stable --no-sandbox --headless --disable-gpu --screenshot http://www.baidu.com/

显示以下即成功:

[1210/000036.689653:INFO:headless_shell.cc(620)] Written to file screenshot.png.

 在爬虫的python程序中:

代码中的chrome_options.add_argument()非常关键,一是要以无界面形式运行,二是禁用沙盒,否则程序报错。

# -*- coding: utf-8 -*-
import time
from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless') # 指定无界面形式运行
chrome_options.add_argument('no-sandbox') # 禁止沙盒
driver = webdriver.Chrome(chrome_options=chrome_options)

driver.get('http://www.baidu.com/')
time.sleep(10)
print(driver.page_source) 

driver.close() 
driver.quit() 

猜你喜欢

转载自www.cnblogs.com/yoyowin/p/12014014.html
今日推荐