python自动下载并更新chromedriver驱动

python用senium调用chrome时,需要使用chromedriver驱动,这个驱动需要对应chrome的版本。
而chrome又经常在后台偷偷升级,如果没关掉升级,之前的驱动又用不了。
其实用webdriver-manager听说也可以,但是他要到国外下载驱动,没办法使用。
这点很愁人,所以我这边开发了一个python自动下载并更新chromedriver驱动的程序。

  • 获取chrome的版本
def get_Chrome_version():
    key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Google\Chrome\BLBeacon')
    version, types = winreg.QueryValueEx(key, 'version')
    return version
  • 获取chromedriver的版本
def get_version(file_path):
    '''查询系统内的Chromedriver版本'''
    outstd2 = os.popen(file_path+'chromedriver --version').read()
    return outstd2.split(' ')[1]

  • 对比及下载
    if driver_main_version != chrome_main_version:
        print("chromedriver版本与chrome浏览器不兼容,更新中>>>")
        versionList = get_server_chrome_versions()
        if chromeVersion in versionList:
            download_url = f"{url}{chromeVersion}/chromedriver_win32.zip"
        else:
            for version in versionList:
                if version.startswith(str(chrome_main_version)):
                    download_url = f"{url}{version}/chromedriver_win32.zip"
                    break
            if download_url == "":
                print("暂无法找到与chrome兼容的chromedriver版本,请在http://npm.taobao.org/mirrors/chromedriver/ 核实。")

        download_driver(download_url=download_url)
        path = file_path
        unzip_driver(path)
        os.remove("chromedriver.zip")

完整程序如下:


import os
import requests
import winreg
import zipfile

url = 'http://npm.taobao.org/mirrors/chromedriver/'


# chromedriver download link
def get_Chrome_version():
    key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Google\Chrome\BLBeacon')
    version, types = winreg.QueryValueEx(key, 'version')
    return version


def get_server_chrome_versions():
    '''return all versions list'''
    versionList = []
    url = "https://registry.npmmirror.com/-/binary/chromedriver/"
    rep = requests.get(url).json()
    for item in rep:
        versionList.append(item["name"])
    return versionList


def download_driver(download_url):
    '''下载文件'''
    file = requests.get(download_url)
    with open("chromedriver.zip", 'wb') as zip_file:  # 保存文件到脚本所在目录
        zip_file.write(file.content)
        print('下载成功')


def get_version(file_path):
    '''查询系统内的Chromedriver版本'''
    outstd2 = os.popen(file_path+'chromedriver --version').read()
    return outstd2.split(' ')[1]


def unzip_driver(path):
    '''解压Chromedriver压缩包到指定目录'''
    f = zipfile.ZipFile("chromedriver.zip", 'r')
    for file in f.namelist():
        f.extract(file, path)


def check_update_chromedriver(file_path):
    chromeVersion = get_Chrome_version()
    chrome_main_version = int(chromeVersion.split(".")[0])  # chrome主版本号
    driver_main_version=''
    if os.path.exists(os.path.join(file_path,"chromedriver.exe")):
        driverVersion = get_version(file_path)
        driver_main_version = int(driverVersion.split(".")[0])  # chromedriver主版本号
    download_url = ""
    if driver_main_version != chrome_main_version:
        print("chromedriver版本与chrome浏览器不兼容,更新中>>>")
        versionList = get_server_chrome_versions()
        if chromeVersion in versionList:
            download_url = f"{url}{chromeVersion}/chromedriver_win32.zip"
        else:
            for version in versionList:
                if version.startswith(str(chrome_main_version)):
                    download_url = f"{url}{version}/chromedriver_win32.zip"
                    break
            if download_url == "":
                print("暂无法找到与chrome兼容的chromedriver版本,请在http://npm.taobao.org/mirrors/chromedriver/ 核实。")

        download_driver(download_url=download_url)
        path = file_path
        unzip_driver(path)
        os.remove("chromedriver.zip")
        print('更新后的Chromedriver版本为:', get_version(file_path))
    else:
        print("chromedriver版本与chrome浏览器相兼容,无需更新chromedriver版本!")
    return os.path.join(file_path,"chromedriver.exe")

if __name__ == "__main__":
    file_path="E:\\"
    print(check_update_chromedriver(file_path))

欢迎点赞及收藏,谢谢!

你可能感兴趣的:(python自动下载并更新chromedriver驱动)