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)
相关推荐
李昊哲小课40 分钟前
Python办公自动化教程 - 第7章 综合实战案例 - 企业销售管理系统
开发语言·python·数据分析·excel·数据可视化·openpyxl
不知名的老吴1 小时前
返回None还是空集合?防御式编程的关键细节
开发语言·python
李昊哲小课1 小时前
Python办公自动化教程 - 第5章 图表创建 - 让数据可视化
python·信息可视化·数据分析·数据可视化·openpyxl
chushiyunen2 小时前
python pygame实现贪食蛇
开发语言·python·pygame
Dream of maid2 小时前
Python-基础2(流程控制)
python
倒酒小生2 小时前
今日算法学习小结
学习
醇氧2 小时前
【学习】【说人话版】子网划分
学习
Lenyiin3 小时前
《Python 修炼全景指南:一》从环境搭建到第一个程序
开发语言·python
不灭锦鲤3 小时前
网络安全学习(面试)
学习·安全·web安全
涛声依旧393163 小时前
Python项目实战:学生信息管理系统
开发语言·python·数据挖掘