0


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'defformat_float(num):return'{:.2f}'.format(num)defdownload_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()definstall_chrome_driver():"""
    安装chrome浏览器
    """try:
        p = ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()
        os.environ['CHROME_DRIVER_PATH']= p
    except Exception as e:print("error")defget_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(" ")iflen(version_list)==3:
                version = version_list[1]returnTrue, version
        else:# 如果执行失败,输出错误信息
            error_message = result.stderr.strip()returnFalse,f"Error: {error_message}"except Exception as e:returnFalse,f"An error occurred: {str(e)}"defget_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:returnNonedefget_chrome_version_info(version_info:str):if os.path.exists(chrome_json_file):withopen(chrome_json_file)as f:
            data = f.read()
            json_data = json.loads(data)if json_data.get(version_info,None)isnotNone:returnTrue, 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)isnotNone:
                        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']withopen(chrome_json_file,'w+')as f:
                    json.dump(dict_version_info, f, indent=4)if dict_version_info.get(version_info,None)isnotNone:
                    version_url = dict_version_info[version_info]returnTrue, version_url
                returnFalse,'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)isnotNone:
                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']withopen(chrome_json_file,'w+')as f:
            json.dump(dict_version_info, f, indent=4)if dict_version_info.get(version_info,None)isnotNone:
            version_url = dict_version_info[version_info]returnTrue, version_url
        returnFalse,'error to get'defdownload_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:ifstr(chrome_driver_version).count(browser_version)>0:print(f"当前已是合适的chrome_driver:{chrome_driver_version}")returnTrueelse:
            chrome_driver_big_version = browser_version.split(".")[0]ifint(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]ifint(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
标签: python

本文转载自: https://blog.csdn.net/Giser_D/article/details/133814392
版权归原作者 Music 爱好者 所有, 如有侵权,请联系我们删除。

“python selenium下载一个合适的chromedriver.exe(稳定版本)”的评论:

还没有评论