python自动更新chromedriver
【摘要】 chrome经常会自动更新,对应的chromedriver也要更新。这样每隔一段时间都很麻烦,下面就是用python程序实现自动更新对应chrome版本的chromedriver的版本。import osimport reimport sysimport winregimport zipfile,timefrom pathlib import Pathimport shutilimport ...
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()
todo:这个是还没有验证,后面有空改一下。
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)