当前位置: 首页 > news >正文

python selenium下载一个合适的chromedriver.exe(稳定版本)

可以使用该脚本来进行下载:
下载前需要安装如下的依赖

requests==2.27.1
selenium==4.14.0
webdriver_manager==4.0.1

下载脚本代码:

import json
import subprocess
import shutil
import os
import time
import zipfile

import requests
from webdriver_manager.core.os_manager import OperationSystemManager
from webdriver_manager.chrome import ChromeDriverManager, ChromeType

__all__ = {
    'download_suit_chrome_driver'
}

# 记录固定数据
chrome_json_file = 'chrome_version.json'
chrome_zip = 'chrome_driver.zip'


def format_float(num):
    return '{:.2f}'.format(num)


def download_file(name, url):
    '''
    :param name:下载保存的名称
    :param url: 下载链接
    :return:
    '''
    headers = {'Proxy-Connection': 'keep-alive'}
    r = requests.get(url, stream=True, headers=headers)
    length = float(r.headers['content-length'])
    f = open(name, 'wb')
    count = 0
    count_tmp = 0
    time1 = time.time()
    for chunk in r.iter_content(chunk_size=512):
        if chunk:
            f.write(chunk)
            count += len(chunk)
            if time.time() - time1 > 2:
                p = count / length * 100
                speed = (count - count_tmp) / 1024 / 1024 / 2
                count_tmp = count
                print(name + ': ' + format_float(p) + '%' + ' Speed: ' + format_float(speed) + 'M/S')
                time1 = time.time()
    f.close()


def install_chrome_driver():
    """
    安装chrome浏览器
    """
    try:
        p = ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()
        os.environ['CHROME_DRIVER_PATH'] = p
    except Exception as e:
        print("error")


def get_chromedriver_version(chromedriver_path="chromedriver.exe"):
    """
    获取chrome_driver版本
    Args:
        chromedriver_path:

    Returns:

    """
    try:
        # 运行Chromedriver,并通过命令行参数获取版本信息
        result = subprocess.run([chromedriver_path, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        if result.returncode == 0:
            # 如果成功执行,解析版本信息
            version = result.stdout.strip()
            version_list = str(version).split(" ")
            if len(version_list) == 3:
                version = version_list[1]
            return True, version
        else:
            # 如果执行失败,输出错误信息
            error_message = result.stderr.strip()
            return False, f"Error: {error_message}"
    except Exception as e:
        return False, f"An error occurred: {str(e)}"


def get_chrome_version():
    """
    获取chrome版本
    Returns:
    """
    try:
        os_version = OperationSystemManager().get_browser_version_from_os("google-chrome")
        print(f"os_version:{os_version}")
        return os_version
    except Exception as e:
        return None


def get_chrome_version_info(version_info: str):
    if os.path.exists(chrome_json_file):
        with open(chrome_json_file) as f:
            data = f.read()
            json_data = json.loads(data)
            if json_data.get(version_info, None) is not None:
                return True, json_data.get(version_info)
            else:
                dict_version_info = {}
                google_driver_json_url = 'https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json'
                res = requests.get(google_driver_json_url)
                res_dict = json.loads(res.text)
                version_list = res_dict['versions']
                for version in version_list:
                    downloads_ = version['downloads']
                    if downloads_.get('chromedriver', None) is not None:
                        download_list = downloads_['chromedriver']
                        for data in download_list:
                            if data['platform'] == 'win64':
                                version_place = str(version['version'])
                                version_ = version_place[0:version_place.rfind('.')]
                                dict_version_info[version_] = data['url']
                with open(chrome_json_file, 'w+') as f:
                    json.dump(dict_version_info, f, indent=4)
                if dict_version_info.get(version_info, None) is not None:
                    version_url = dict_version_info[version_info]
                    return True, version_url
                return False, 'error to get'

    else:
        dict_version_info = {}
        google_driver_json_url = 'https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json'
        res = requests.get(google_driver_json_url)
        res_dict = json.loads(res.text)
        version_list = res_dict['versions']
        for version in version_list:
            downloads_ = version['downloads']
            if downloads_.get('chromedriver', None) is not None:
                download_list = downloads_['chromedriver']
                for data in download_list:
                    if data['platform'] == 'win64':
                        version_place = version['version']
                        version_ = version_place[0:version_place.rfind('.')]
                        dict_version_info[version_] = data['url']
        with open(chrome_json_file, 'w+') as f:
            json.dump(dict_version_info, f, indent=4)
        if dict_version_info.get(version_info, None) is not None:
            version_url = dict_version_info[version_info]
            return True, version_url
        return False, 'error to get'


def download_suit_chrome_driver(chrome_driver_path: str = "chromedriver.exe"):
    """
    下载合适的chrome_driver.exe
    Returns:
    """
    is_ok, chrome_driver_version = get_chromedriver_version(chrome_driver_path)
    browser_version = get_chrome_version()
    if is_ok:
        if str(chrome_driver_version).count(browser_version) > 0:
            print(f"当前已是合适的chrome_driver:{chrome_driver_version}")
            return True
        else:
            chrome_driver_big_version = browser_version.split(".")[0]
            if int(chrome_driver_big_version) < 115:
                print("下载Chrome-Driver")
                install_chrome_driver()
            else:
                is_get_chrome, version_info = get_chrome_version_info(browser_version)
                if is_get_chrome:
                    download_url = version_info
                    print('ok-remove-0')
                    if os.path.exists(chrome_zip):
                        os.remove(chrome_zip)
                    print('ok-remove-2')
                    download_file(chrome_zip, download_url)
                    print('ok-remove-3')
                    with zipfile.ZipFile(chrome_zip, 'r') as zip_ref:
                        zip_ref.extractall('./')
                    shutil.move('./chromedriver-win64/chromedriver.exe', './chromedriver.exe')
                    if os.path.exists(chrome_zip):
                        os.remove(chrome_zip)
    else:
        chrome_driver_big_version = browser_version.split(".")[0]
        if int(chrome_driver_big_version) < 115:
            print("下载Chrome-Driver")
            install_chrome_driver()
        else:
            is_get_chrome, version_info = get_chrome_version_info(browser_version)
            if is_get_chrome:
                download_url = version_info
                print('ok-remove-0')
                if os.path.exists(chrome_zip):
                    os.remove(chrome_zip)
                print('ok-remove-2')
                download_file(chrome_zip, download_url)
                print('ok-remove-3')
                with zipfile.ZipFile(chrome_zip, 'r') as zip_ref:
                    zip_ref.extractall('./')
                shutil.move('./chromedriver-win64/chromedriver.exe', './chromedriver.exe')
                if os.path.exists(chrome_zip):
                    os.remove(chrome_zip)


if __name__ == '__main__':
    download_suit_chrome_driver("chromedriver.exe")

调用方式:

  download_suit_chrome_driver("xxxxx")  ## xxxxxx表示chrome_driver.exe路径(可为空)

github下载链接: https://github.com/huifeng-kooboo/download_chrome_driver

相关文章:

  • 【数据结构】:二叉树与堆排序的实现
  • acwing算法基础之基础算法--整数离散化算法
  • vue打包压缩
  • Go语言入门心法(三): 接口
  • 端到端的机器学习项目(Machine Learning 研习之六)
  • 深入了解 JavaScript 中的构造函数和对象创建
  • 最新ai创作系统CHATGPT系统源码+支持GPT4.0+支持ai绘画(Midjourney)
  • 海康威视、大华、宇视rtsp实时读取网络摄像头
  • c++string类的赋值问题
  • Android Handler/Looper视角看UI线程的原理
  • 1.12.C++项目:仿muduo库实现并发服务器之LoopThreadPool模块的设计
  • PCL点云处理之从两片点云中获取具有匹配关系的同名点对 (二百一十八)
  • 【音视频|ALSA】SS528开发板编译Linux内核ALSA驱动、移植alsa-lib、采集与播放usb耳机声音
  • vsc连接wsl安装vsc时遇到权限问题的解决方案
  • 如何使用ChatPPT生成PPT文档
  • SpringCloud-Gateway
  • LENOVO联想笔记本小新 Pro-14 2021AMD处理器ACH版(82MS)原厂Win10系统
  • ElementUI编辑表格单元格与查看模式切换的应用
  • 算法解析:LeetCode——机器人碰撞和最低票价
  • Qt工具开发,该不该跳槽?
  • 比特币价格时隔三个月再度站上10万美元
  • 扶桑谈|素称清廉的石破茂被曝受贿,日本政坛或掀起倒阁浪潮
  • 万里云端遇见上博--搭乘“上博号”主题飞机体验记
  • 同观·德国|默茨当总理后,能否带领德国在欧盟“说了算”?
  • 国新办将于5月8日10时就《民营经济促进法》有关情况举行新闻发布会
  • 习近平给谢依特小学戍边支教西部计划志愿者服务队队员的回信