chromedriver自動アップデート

Pythonで自動的にchromedriverアップデート処理。

例外処理など全てなし、必要な部分のみ。

# coding: utf-8

import os
from webdriver_manager.utils import chrome_version
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import urllib.request
import zipfile

# ファイルパス
zipfilepath = r'配置パス\chromedriver_win32.zip'
exefilepath = r'配置パス\chromedriver.exe'

# chromeバージョン
chrome_version = chrome_version()

# ダウンロードURL取得
url = 'https://chromedriver.chromium.org/downloads'
download_url = None

html = urlopen(url).read()
soup = BeautifulSoup(html, 'html.parser')
elements = soup.select('html body a')
pattern = r"^[0-9]{2}[.]+[0-9]{1,2}[.]+[0-9]{4}$"
for element in elements:
	if ('ChromeDriver' in element.text):
		vd = element.text.replace('ChromeDriver', '').strip()[0:len(chrome_version)]

		if (re.match(pattern, vd) and chrome_version >= vd):
			download_url = element.get('href')
			break

# ダウンロード
download_url = download_url.replace('index.html?path=', '') + os.path.basename(zipfilepath)
urllib.request.urlretrieve(download_url, zipfilepath)

# 解凍
with zipfile.ZipFile(zipfilepath) as existing_zip:
	existing_zip.extract('chromedriver.exe', os.path.dirname(exefilepath))

poplibで利用したメール受信

# coding: utf-8

import time
import poplib
import email
from email.header import decode_header

# ヘッダ情報取得
def get_header(msg, name):
	header = ''
	if msg[name]:
		for tup in decode_header(str(msg[name])):
			if type(tup[0]) is bytes:
				charset = tup[1]
				if charset:
					header += tup[0].decode(tup[1])
				else:
					header += tup[0].decode()
			elif type(tup[0]) is str:
					header += tup[0]
	return header

# 受信日時
def get_date(msg):
	mdate = email.utils.parsedate(msg.get('date'))
	return time.strftime('%Y/%m/%d %H:%M:%S', mdate)

# body情報
def get_content(msg):
	if msg.is_multipart() is True:
		rst = ""
		for part in msg.walk():
			payload = part.get_payload(decode=True)
			if payload is None:
				continue
			charset = part.get_content_charset()
			if charset is not None:
				payload = payload.decode(charset, "ignore")
			rst += str(payload)
		return rst
	else:
		charset = msg.get_content_charset()
		payload = msg.get_payload(decode=True)
		try:
			if payload:
				if charset:
					return payload.decode(charset)
				else:
					return payload.decode()
			else:
				return ""
		except:
			return payload

# サーバに接続
cli = poplib.POP3('www.example.com')

# 認証
cli.user('ユーザID')
cli.pass_('パスワード')

# メールボックス内のメールの総数を取得
count = cli.stat()[0]

for i in range(count):
	no = i + 1
	content = cli.retr(no)[1]
	msg = email.message_from_bytes(b'\r\n'.join(content))

	print(get_header(msg, 'from'))
	print(get_header(msg, 'subject'))
	print(get_date(msg))
	print(get_content(msg))

	# 削除
	# cli.dele(no)

Pythonをexe化

pyinstaller ファイル.py <オプション>

    1. –onefile / -F
      出力ファイルを1つにまとめる
    2. –noconsole / -w / –windowed
      コンソールなし
    3. –icon / -i
      アイコン指定
    4. –name / -n
      exe名指定
    5. –clean
      キャッシュ削除