こちらの内容がより新しいです。
Windows10, Python3 用。ユーザーランドに Chromedriver がおいてある設定。
webdriver.Chrome を立ち上げる前に入れておくとバージョンのチェックしてバージョンが合わない場合は合う Chromedriver に置き換えてくれるようになっているはず。
令和3年5月31日改定2
こっちのほうがいいかも
import os import re import requests import zipfile import urllib.request from lxml import html def chrome_driver_auto_update(chromedriver_path = os.path.expanduser("~")): pattern = r'\d+\.\d+\.\d+' cmd = r'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version' stdout = os.popen(cmd).read() version = re.search(pattern, stdout)[0].split('.')[0] cmd2 = rf'{os.path.join(chromedriver_path,"chromedriver.exe")} -version' stdout2 = subprocess.check_output(cmd2) if version!=stdout2.split()[1].decode().split('.')[0]: url = f'https://chromedriver.storage.googleapis.com/LATEST_RELEASE_{version}' with urllib.request.urlopen(url) as f: htmltext = f.read().decode('utf-8') furl = f"https://chromedriver.storage.googleapis.com/{htmltext}/chromedriver_win32.zip" urllib.request.urlretrieve(furl, "temp.zip") with zipfile.ZipFile('temp.zip') as zipF: zipF.extractall(chromedriver_path) chrome_driver_auto_update()
令和3年5月31日改定
with 多めでサイトの改変に対応。
import os import re import requests import zipfile import urllib.request from lxml import html def chrome_driver_auto_update(chromedriver_path = os.path.expanduser("~")): pattern = r'\d+\.\d+\.\d+' cmd = r'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version' stdout = os.popen(cmd).read() version = re.search(pattern, stdout)[0].split('.')[0] cmd2 = rf'{os.path.join(chromedriver_path,"chromedriver.exe")} -version' stdout2 = subprocess.check_output(cmd2) if version!=stdout2.split()[1].decode().split('.')[0]: url = 'https://chromedriver.chromium.org/downloads' with urllib.request.urlopen(url) as f: htmltext = f.read().decode('utf-8') xpath = "/html/body/div[1]/div/div[2]/div[2]/div[1]/section[2]/div[2]/div/div/div/div/div/div/div/div" title = html.fromstring(htmltext).xpath(xpath) for s in title: for ss in s.xpath('p/span/a/@href'): ahref=re.findall(r"https://chromedriver.storage.googleapis.com/index.html\?path=[0-9.]+",ss) if len(ahref)>0: if re.findall("[0-9]+",ahref[0])[0]==version: furl=ahref[0].replace('index.html?path=','') + "/chromedriver_win32.zip" urllib.request.urlretrieve(furl, "temp.zip") with zipfile.ZipFile('temp.zip') as zipF: zipF.extractall(chromedriver_path) chrome_driver_auto_update()
原文
with 少なめにしたかったので requests と urllib.request の両方を import し、内包表記とスライス多用していますが練習用ということで。
import os import requests import zipfile import urllib.request from lxml import html def chrome_driver_auto_update(chromedriver_dir_path = os.path.expanduser("~")): cmd = r'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version' stdout = os.popen(cmd).read() version = stdout.split()[3].split('.')[0] cmd2 = rf'{os.path.join(chromedriver_dir_path,"chromedriver.exe")} -version' stdout2 = os.popen(cmd2).read() if version != stdout2.split()[1].split('.')[0]: chrome_driver_page_url = 'https://chromedriver.chromium.org/downloads' htmltext = requests.get(chrome_driver_page_url).text title = html.fromstring(htmltext).xpath('//*[@id="sites-canvas-main-content"]/table/tbody/tr/td/div/div[1]/ul/li') index_number=[i.xpath('a')[0].text.split()[1].split('.')[0] for i in title[:3]].index(version) chrome_driver_url = [i.xpath('a')[0].attrib['href'] for i in title[:3]][index_number].replace('index.html?path=','') + "chromedriver_win32.zip" urllib.request.urlretrieve(chrome_driver_url, "chromedriver_win32.zip") zipF = zipfile.ZipFile('chromedriver_win32.zip') zipF.extractall(chromedriver_dir_path) zipF.close() chrome_driver_auto_update()
こちらのサイトを参考に作成しました。ありがとうございました。
mio.yokohama