python自动更新chromedriver

简介: python自动更新chromedriver

chrome经常会自动更新,对应的chromedriver也要更新。这样每隔一段时间都很麻烦,下面就是用python程序实现自动更新对应chrome版本的chromedriver的版本。

import os
import re
import sys
import winreg
import zipfile,time
from pathlib import Path
import shutil
import requests

python_root = Path(sys.executable).parent  # python安装目录

version_re = re.compile(r'^[1-9]\d*\.\d*.\d*')  # 匹配前3位版本信息


def get_chrome_version():
    """通过注册表查询Chrome版本信息: HKEY_CURRENT_USER\SOFTWARE\Google\Chrome\BLBeacon: version"""
    try:
        key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'SOFTWARE\Google\Chrome\BLBeacon')
        value = winreg.QueryValueEx(key, 'version')[0]
        return version_re.findall(value)[0]
    except WindowsError as e:
        return '0.0.0'  # 没有安装Chrome浏览器


def get_chrome_driver_version():
    try:
        result = os.popen('chromedriver --version').read()
        version = result.split(' ')[1]
        return '.'.join(version.split('.')[:-1])
    except Exception as e:
        return '0.0.0'  # 没有安装ChromeDriver


def get_latest_chrome_driver(chrome_version):#使用淘宝镜像下载安装Chromedriver   2023年11月失效
    base_url = 'http://npm.taobao.org/mirrors/chromedriver/'  # chromedriver在国内的淘宝镜像网站
    url = f'{base_url}LATEST_RELEASE_{chrome_version}'
    latest_version = requests.get(url).text        
    download_url = f'{base_url}{latest_version}/chromedriver_win64.zip'        

    # 下载chromedriver zip文件到Python 根目录
    response = requests.get(download_url)    
    local_file = python_root / 'chromedriver.zip'
    with open(local_file, 'wb') as zip_file:
        zip_file.write(response.content)

    # 解压缩zip文件到python安装目录
    f = zipfile.ZipFile(local_file, 'r')
    for file in f.namelist():
        f.extract(file, python_root)
    f.close()

    local_file.unlink()  # 解压缩完成后删除zip文件
def get_chrome_driver_fromGithub(chrome_version):#使用github仓库上的Chromedriver
    base_url = f'https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_STABLE'
    latest_version = requests.get(base_url).text#查询最新的state版本号
    print(f"GitHub库里最新完整版driver = {latest_version}")
    download_url = f'https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/{latest_version}/win64/chromedriver-win64.zip'
    # 下载chromedriver zip文件到Python 根目录
    response = requests.get(download_url)    
    local_file = python_root / 'chromedriver.zip'
    with open(local_file, 'wb') as zip_file:
        zip_file.write(response.content)

    # 解压缩zip文件到python安装目录
    f = zipfile.ZipFile(local_file, 'r')    
    f.extract('chromedriver-win64/chromedriver.exe', python_root)    
    f.close()

    local_file.unlink()  # 解压缩完成后删除zip文件
    # 从chromedriver-win64目录移动Chromedriver.exe 
    os.remove(f"{python_root}/chromedriver.exe")
    shutil.move(f"{python_root}/chromedriver-win64/chromedriver.exe",python_root)
def check_chrome_driver_update():
    chrome_version = get_chrome_version()
    driver_version = get_chrome_driver_version()
    print(f'chrome_version = {chrome_version},driver_version = {driver_version}')
    if chrome_version == driver_version:
        print('No need to update')
    else:
        try:
            get_chrome_driver_fromGithub(chrome_version)
        except Exception as e:
            print(f'GitHub仓库Fail to update: {e}')
            try:
                get_latest_chrome_driver(chrome_version)
            except Exception as e:
                print(f'淘宝镜像站Fail to update:{e}')
            time.sleep(20)
        else:
            print("Success to download chrome_driver.")


if __name__ == '__main__':
    print(python_root)
    check_chrome_driver_update()

todo:这个是还没有验证,后面有空改一下。

目录
相关文章
|
3月前
|
Python
python selenium 执行完毕关闭chromedriver进程
python selenium 执行完毕关闭chromedriver进程
50 0
|
Python
python-ui之tkinter初步学习-制作自动更新时间的闹钟
python-ui之tkinter初步学习-制作自动更新时间的闹钟
132 0
|
Web App开发 测试技术 网络安全
怎么利用 ChromeDriver 和 Selenium对 CEF应用进行自动化测试-python实现
怎么利用 ChromeDriver 和 Selenium对 CEF应用进行自动化测试-python实现
465 0
|
JavaScript 前端开发 Python
python3模拟提交问卷星/问卷网表单(selenium+chromedriver/phantomjs)
前一段时间有个课程需要问卷星搜集材料信息,,但是问卷星这东西你不一个个求人哪有人愿意点进去帮你填呢,,呵呵,不行,我自己来。。。
190 0
python3模拟提交问卷星/问卷网表单(selenium+chromedriver/phantomjs)
|
Web App开发 iOS开发 Python
python小技能: 【自动回复社区帖子的留言】搭建selenium与ChromeDriver环境教程(Mac版本)
python小技能: 【自动回复社区帖子的留言】搭建selenium与ChromeDriver环境教程(Mac版本)
382 0
python小技能: 【自动回复社区帖子的留言】搭建selenium与ChromeDriver环境教程(Mac版本)
|
JavaScript 数据安全/隐私保护 Python
Python 调用 selenium chromedriver 常用配置
话不多说,上示例代码 from selenium import webdriver import time chromedriver = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.
1424 0
|
8天前
|
算法 程序员 开发工具
百万级Python讲师又一力作!Python编程轻松进阶,豆瓣评分8.1
在学习Python的旅程中你是否正在“绝望的沙漠”里徘徊? 学完基础教程的你,是否还在为选择什么学习资料犹豫不决,不知从何入手,提高自己?
百万级Python讲师又一力作!Python编程轻松进阶,豆瓣评分8.1
|
1天前
|
Shell 数据处理 C++
【震撼揭秘】Python正则VS Shell正则:一场跨越编程边界的史诗级对决!你绝不能错过的精彩较量,带你领略文本处理的极致魅力!
【8月更文挑战第19天】正则表达式是文本处理的强大工具,在Python与Shell中有广泛应用。两者虽语法各异,但仍共享许多基本元素,如`.`、`*`及`[]`等。Python通过`re`模块支持丰富的功能,如非捕获组及命名捕获组;而Shell则依赖`grep`、`sed`和`awk`等命令实现类似效果。尽管Python提供了更高级的特性和函数,Shell在处理文本文件方面仍有其独特优势。选择合适工具需根据具体需求和个人偏好决定。
|
6天前
|
算法 程序员 开发工具
百万级Python讲师又一力作!Python编程轻松进阶,豆瓣评分8.1
在学习Python的旅程中你是否正在“绝望的沙漠”里徘徊? 学完基础教程的你,是否还在为选择什么学习资料犹豫不决,不知从何入手,提高自己?
|
3天前
|
数据采集 存储 人工智能
掌握Python编程:从基础到进阶的实用指南
【8月更文挑战第17天】 本文旨在通过浅显易懂的语言和实际案例,为初学者和有一定基础的开发者提供一条清晰的Python学习路径。我们将从Python的基本语法入手,逐步深入到面向对象编程、数据科学应用及网络爬虫开发等高级主题。每个部分都配备了代码示例和实操建议,确保读者能够将理论知识转化为实际能力。无论你是编程新手,还是希望提升Python技能的开发者,这篇文章都将为你打开一扇通往高效编程世界的大门。
7 2