# -*- coding:utf-8 -*-
import json
import os
import time
import ssl
import requests
import winreg
import zipfile
from requests.packages.urllib3.exceptions import InsecureRequestWarning
"""忽略SSL证书警告"""
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
ssl._create_default_https_context = ssl._create_unverified_context
"""ChromeDriver仓库淘宝镜像地址"""
# ChromeDriver_depot_url = r'http://npm.taobao.org/mirrors/chromedriver/'
ChromeDriver_depot_url = r'https://registry.npmmirror.com/binary.html?path=chromedriver/'
ChromeDriver_base_url = r'https://registry.npmmirror.com/-/binary/chromedriver/'
def get_Chrome_version():
"""
通过注册表的方式获取Google Chrome的版本
:return: 本机Chrome的版本号(如:96.0.4664)
"""
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Google\Chrome\BLBeacon')
version, types = winreg.QueryValueEx(key, 'version')
print("本机目前的Chrome版本为:", version)
return version
def get_version():
"""
查询系统内的Chromedriver版本
:return: 本机ChromeDriver的版本(如:92.0.4515)
"""
ChromeDriverVersion = os.popen('chromedriver --version').read()
print("本机目前的Chromedriver版本为:", ChromeDriverVersion.split(' ')[1])
return ChromeDriverVersion.split(' ')[1]
def get_server_chrome_versions(url):
"""
获取ChromeDriver版本仓库中的所有版本并写入列表
:param: 淘宝的ChromeDriver仓库地址
:return: versionList 版本列表
"""
versionList = []
rep = requests.get(url, verify=False).text
rep_list = json.loads(rep)
for i in range(len(rep_list)):
version = rep_list[i]['name'] # 提取版本号
versionList.append(version[:-1]) # 将所有版本存入列表
return versionList
def download_driver(download_url):
"""
下载文件
:param download_url: ChromeDriver对应版本下载地址
"""
file = requests.get(download_url, verify=False)
with open("chromedriver.zip", 'wb') as zip_file: # 保存文件到脚本所在目录
zip_file.write(file.content)
print('下载成功')
def unzip_driver(path):
"""
解压Chromedriver压缩包到指定目录
:param path: 指定解压目录
"""
f = zipfile.ZipFile("chromedriver.zip", 'r')
for file in f.namelist():
f.extract(file, path)
def get_path():
"""
获取当前ChromeDriver的存放路径
:return: ChromeDriver当前路径
"""
ChromeDriverLocating = os.popen('where chromedriver').read()
ChromeSavePath, ChromeName = os.path.split(ChromeDriverLocating)
return ChromeSavePath
def check_update_chromedriver():
chromeVersion = get_Chrome_version()
chrome_main_version = int(chromeVersion.split(".")[0]) # chrome主版本号
try:
driverVersion = get_version()
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(ChromeDriver_base_url)
if chromeVersion in versionList:
download_url = f"{ChromeDriver_base_url}{chromeVersion}/chromedriver_win32.zip"
else:
for version in versionList:
if version.startswith(str(chrome_main_version)):
download_url = f"{ChromeDriver_base_url}{version}/chromedriver_win32.zip"
break
if download_url == "":
print(
r"暂无法找到与chrome兼容的chromedriver版本,请在http://npm.taobao.org/mirrors/chromedriver/ 核实。")
download_driver(download_url=download_url)
Chrome_Location_path = get_path()
print("解压地址为:", Chrome_Location_path)
unzip_driver(Chrome_Location_path)
os.remove("chromedriver.zip")
print('更新后的Chromedriver版本为:', get_version())
else:
print(r"chromedriver版本与chrome浏览器相兼容,无需更新chromedriver版本!")
except:
driverVersion = ''
print("chromedriver不存在")
print("chromedriver需重新下载")
print("正在下载……")
download_url == ""
versionList = get_server_chrome_versions(ChromeDriver_base_url)
if chromeVersion in versionList:
download_url = f"{ChromeDriver_base_url}{chromeVersion}/chromedriver_win32.zip"
else:
for version in versionList:
if version.startswith(str(chrome_main_version)):
download_url = f"{ChromeDriver_base_url}{version}/chromedriver_win32.zip"
break
if download_url == "":
print(
r"暂无法找到与chrome兼容的chromedriver版本,请在http://npm.taobao.org/mirrors/chromedriver/ 核实。")
download_driver(download_url=download_url)
ChromeDriverLocating = os.popen('where python').read().split('\n')[0]
ChromeSavePath, ChromeName = os.path.split(ChromeDriverLocating)
print("解压地址为:", ChromeSavePath)
unzip_driver(ChromeSavePath)
os.remove("chromedriver.zip")
print('更新后的Chromedriver版本为:', get_version())
if __name__ == "__main__":
check_update_chromedriver()
time.sleep(10)
selenium——Chromedriver的自动更新和下载
最新推荐文章于 2024-11-08 09:41:27 发布