1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Seleniumを使ったWebページのPDF化

Last updated at Posted at 2023-02-15

事前準備

# 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))
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?