事前準備
# pip install selenium
# pip install webdriver-manager
1つのURLだけの場合
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {
'download.default_directory': '~/Downloads'
})
options.add_argument('--kiosk-printing')
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver.get("ここにURLを入れる")
driver.execute_script('window.print();')
driver.quit()
複数のURLの場合
- テキストファイルにURLを1行ずつ入れておく
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import random
from time import sleep
# オプション設定
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {
'download.default_directory': '~/Downloads'
})
options.add_argument('--kiosk-printing')
with open("URLのリスト") as urls:
# ドライバ起動
for url in urls.read().split("\n"):
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver.get(url)
sleep(random.randint(3, 6))
driver.execute_script('window.print();')
driver.quit()
sleep(random.randint(3, 6))