python爬虫学习之selenium_chrome handless的使用

目录

[一、Chrome handless简介](#一、Chrome handless简介)

[二、Chrome handless的系统要求](#二、Chrome handless的系统要求)

[三、Chrome handless的基本配置 (直接复制放在.py文件开头)](#三、Chrome handless的基本配置 (直接复制放在.py文件开头))

[四、Chrome handless 的应用](#四、Chrome handless 的应用)

[五、Chrome handless的封装](#五、Chrome handless的封装)


一、Chrome handless简介

Chrome handless 模式,Google 针对 Chrome 浏览器 59版 新增的一种模式,可以让你不打开 UI 界面的情况下使用 Chrome 浏览器,所以运行效果与 Chrome 保持完美一致。

二、Chrome handless的系统要求

1、Chrome 版本要求

Unix/Linux 系统要求 chrome >= 59

Windows 系统需要 chrome >= 60

2、Python 版本 >= 3.6

3、Selenium 版本 >= 3.4.*

4、ChromeDriver 版本 >= 2.31

三、Chrome handless的基本配置 (直接复制放在.py文件开头)

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options. add_argument('--headless')

#path是自己电脑的Chrome浏览器文件地址
path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
chrome_options.binary_location = path

browser = webdriver.Chrome(options=chrome_options)

四、Chrome handless 的应用

e.g.访问百度网站

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options. add_argument('--headless')

#path是自己电脑的Chrome浏览器文件地址
path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
chrome_options.binary_location = path

browser = webdriver.Chrome(options=chrome_options)

url = 'https://www.baidu.com'

browser.get(url)

注:chrome_options已经被options替代

五、Chrome handless的封装

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def common_browser():
    chrome_options = Options()
    chrome_options.add_argument('--headless')

    # path是自己电脑的Chrome浏览器文件地址
    path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
    chrome_options.binary_location = path

    browser = webdriver.Chrome(options=chrome_options)
    return browser

注:使用时直接调用函数

复制代码
browser = common_browser()

url = 'https://www.baidu.com'

browser.get(url)
相关推荐
databook4 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar5 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户8356290780515 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_5 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
子竹聆风7 小时前
Feapder框架UpdateItem使用技巧:如何优雅地实现"只更新有值字段"
爬虫
数据智能老司机12 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机13 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机13 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机13 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i13 小时前
drf初步梳理
python·django