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))
歡迎點贊及收藏,謝謝!