selenium+headless chrome安装使用

pip install selenium

因为phantomJS将停止维护,所以建议使用headless chrome
ChromeDriver is a separate executable that WebDriver uses to control Chrome.

1、确保谷歌浏览器安装在可以找到的位置(默认位置或自己指定的位置)。
如果不是默认位置,则需要用下面的代码来指定谷歌浏览器的安装位置:
ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/other/chrome/binary");

2、下载你系统上所需要的ChromeDriver文件,windows所需下载地址为:
https://chromedriver.storage.googleapis.com/index.html?path=2.35/

3、帮助WebDriver找到你下载的ChromeDriver文件:
将ChromeDriver文件存放在PATH目录下或

from selenium import webdriver
driver = webdriver.Chrome('/path/to/chromedriver')

4、(可选)启动和退出ChromeDriver server需要一些时间,所以提供了两种方法来
解决这个问题:
1、使用ChromeDriverService
2、作为一个服务器单独启动ChromeDriver server,然后用Remote WebDriver连接它。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://www.baidu.com")
print(driver.title)
driver.quit()

参考链接:
https://sites.google.com/a/chromium.org/chromedriver/home 介绍地址
https://sites.google.com/a/chromium.org/chromedriver/getting-started 入门地址

posted @ 2018-01-15 16:32  zxpo  阅读(2128)  评论(0编辑  收藏  举报