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)
相关推荐
baopixiaoz9 分钟前
BeeQuant × BeeAgent:AI驱动智能交易
大数据·人工智能·python·区块链
公爵爱学习19 分钟前
无人机定点飞行-介绍
开发语言·图像处理·python·学习·线性回归·无人机
浔溺19 分钟前
al+大数据每日学习笔记33
大数据·笔记·学习
stillstream_ink19 分钟前
OpenCart UI 自动化测试实战:POM 四层架构 + 失败自动截图 + 飞书通知
python·测试
databook22 分钟前
Beta 分布:如何用“成功”和“失败”来预测下次尝试
python·数据挖掘·数据分析
zhanghaha131431 分钟前
Python进阶教程:27_subprocess 模块 零基础超详细教程
开发语言·python
兄弟加油,别颓废了。33 分钟前
bugku题目
开发语言·前端·python
新网企兴33 分钟前
2026西安GEO推广选哪家?新网企兴帮您精准获客
人工智能·python
lifallen41 分钟前
Agent 框架不是 API 包装器,而是一个微型操作系统内核
人工智能·学习·ai·ai编程
2601_962297481 小时前
一、Python GUI 的十字路口:免费凑活还是付费破局
python·gui·tkinter·customtkinter·bravurasdk