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)
相关推荐
LFly_ice17 分钟前
学习React-24-路由传参
前端·学习·react.js
陈天伟教授1 小时前
基于学习的人工智能(3)机器学习基本框架
人工智能·学习·机器学习·知识图谱
毕设源码-钟学长1 小时前
【开题答辩全过程】以 高校课程学习评价系统设计与实现为例,包含答辩的问题和答案
学习
fruge3 小时前
从第三方库中偷师:学习 Lodash 的函数封装技巧
学习
笨笨聊运维5 小时前
CentOS官方不维护版本,配置python升级方法,无损版
linux·python·centos
Gerardisite5 小时前
如何在微信个人号开发中有效管理API接口?
java·开发语言·python·微信·php
lingggggaaaa6 小时前
免杀对抗——C2远控篇&C&C++&DLL注入&过内存核晶&镂空新增&白加黑链&签名程序劫持
c语言·c++·学习·安全·网络安全·免杀对抗
小毛驴8506 小时前
软件设计模式-装饰器模式
python·设计模式·装饰器模式