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()