import os
import requests
from selenium import webdriver
import re
import zipfile
def un_zip(file_name_path, file_path):
"""unzip zip file"""
folder_path = file_path + file_name_path.split('\\')[-1].split('.')[0]
# 解压文件,创建一个zip_file对象
zip_file = zipfile.ZipFile(file_name_path)
try:
if os.path.isdir(folder_path):
None
else:
os.mkdir(folder_path)
for names in zip_file.namelist():
zip_file.extract(names, folder_path)
print(file_name_path, '解压成功')
except Exception as result:
print("文件名重复,报错:\n%s\n请查看文件是否已解压" % result)
def get_chromedriver_state(path):
try:
chrome_options = webdriver.ChromeOptions()
chromedriver_path = path + r'chromedriver_win32\\chromedriver.exe'
webdriver.Chrome(executable_path=chromedriver_path, options=chrome_options)
return {'state': '1', 'explain': '该版本可使用', 'path': path + r'chromedriver_win32\\chromedriver.exe'}
except Exception as e:
print('e:', e)
if "'chromedriver.exe' executable needs to be in PATH" in str(e):
print('未检到chromedriver.exe文件,开始下载旧版未检到chromedriver!')
url = 'http://chromedriver.storage.googleapis.com/99.0.4844.35/chromedriver_win32.zip'
r = requests.get(url).content
open(path + 'chromedriver_win32.zip', 'wb+').write(r)
un_zip(path + 'chromedriver_win32.zip', path)
return get_chromedriver_state(path)
version_number = re.findall(r'browser version is (.*) with binary', str(e))[0]
print('检测到chrome版本为:', version_number)
print('开始下载压缩文件到:', path)
url = 'http://chromedriver.storage.googleapis.com/' + version_number + '/chromedriver_win32.zip'
print('下载链接:', url)
r = requests.get(url).content
open(path + 'chromedriver_win32.zip', 'wb+').write(r)
un_zip(path + 'chromedriver_win32.zip', path)
return {'state': '1', 'explain': '文件已更新', 'path': path + r'chromedriver_win32\\chromedriver.exe'}
if __name__ == '__main__':
print(get_chromedriver_state('cs\\'))
根据当前谷歌浏览器版本获取或更新更新chromedriver.exe
于 2022-07-29 09:09:11 首次发布